I'm trying to create a webpage where by I have the questions in the database instead of it being hard code which is writing it down in php. How do i go about that. Just a simple page which will have a question and a blank box where the answer is meant to be written and the answer would also be stored in the database. Please any help would be really appreciated. I hope you understand what i'm trying to say

Simple. Fetch all the questions from the table and print it as follows.

$query="Select question from question_table";
$result=mysql_query($query);
$i=1;
echo "<form method=\"post\" action=\"questionpaper.php\">";
while($row=mysql_fetch_array($result)){
  $question=$row['question'];
  echo $i."." $question."<br />"; // to print 1. What is your name ?
  echo "<input type=\"text\" name=\"answer$i\" /><br />"; \\ to have unique textboxes for different questions
$i++;
}
echo "<input type=\"hidden\" name=\"count\" value=\"$i\">"; \\ to count the number of questions
echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
?>

When the user clicks on submit, get the value of i, loop through the answers.

$i=$_REQUEST['count'];
for($j=1;$j<$i;$j++){
   $answertextbox="answer".$j;
  $answer=$_POST[$answertextbox];
//insert $answer to the table
} //by the end of for loop, all the answers will be in the table.
?>

cheers,
Naveen

thanks nav. How do i go about this. Now that you have given me the code. What do i do to make it all work. Do i need to put anything into the MySQL table as i have a database already

Create a table with all the questions. You need another table to store the answers. question_id would be the foreign key in answers table. If you are doing this question-answer script for multiple users, you need another table users, whose 'user_id' would be a foreign key for table answers.
So, questions table will have
1. question_id, primary key
2. question
Answer table will have
1. answer_id, primary key
2. question_id, foreign key from questions table
3. user_id, foreign key from users table
Users table
1. user_id , primary key
and the user details

hi nav,
I have created the tables now. What do i do next? Do i just put in the code you gave me in notepad and then save as a php file?

umm.. The code that I have given you is just an example how you can do it. Change it as per your needs. List all the questions with a textbox(with different names).You may also need to save the question_ids to store it in the answer table. When the submit button is pressed, get all the answers and the question_id's (for that particular user) and save it in the table.

ok..thank you, i'll try and see how far i can get on with this

You are welcome! :) Its easy. Just keep trying.. You ll get it. Best of luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.