User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,665 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,876 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 232 | Replies: 7 | Solved
Reply
Join Date: Oct 2006
Posts: 88
Reputation: dami06 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dami06 dami06 is offline Offline
Junior Poster in Training

Storing questions in the database

  #1  
Feb 13th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Storing questions in the database

  #2  
Feb 13th, 2008
Simple. Fetch all the questions from the table and print it as follows.
  1. $query="Select question from question_table";
  2. $result=mysql_query($query);
  3. $i=1;
  4. echo "<form method=\"post\" action=\"questionpaper.php\">";
  5. while($row=mysql_fetch_array($result)){
  6. $question=$row['question'];
  7. echo $i."." $question."<br />"; // to print 1. What is your name ?
  8. echo "<input type=\"text\" name=\"answer$i\" /><br />"; \\ to have unique textboxes for different questions
  9. $i++;
  10. }
  11. echo "<input type=\"hidden\" name=\"count\" value=\"$i\">"; \\ to count the number of questions
  12. echo "<input type=\"submit\" name=\"submit\" value=\"submit\">";
  13. ?>
When the user clicks on submit, get the value of i, loop through the answers.
  1. $i=$_REQUEST['count'];
  2. for($j=1;$j<$i;$j++){
  3. $answertextbox="answer".$j;
  4. $answer=$_POST[$answertextbox];
  5. //insert $answer to the table
  6. } //by the end of for loop, all the answers will be in the table.
  7. ?>

cheers,
Naveen
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2006
Posts: 88
Reputation: dami06 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dami06 dami06 is offline Offline
Junior Poster in Training

Re: Storing questions in the database

  #3  
Feb 13th, 2008
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
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Storing questions in the database

  #4  
Feb 13th, 2008
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
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2006
Posts: 88
Reputation: dami06 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dami06 dami06 is offline Offline
Junior Poster in Training

Re: Storing questions in the database

  #5  
Feb 13th, 2008
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?
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Storing questions in the database

  #6  
Feb 13th, 2008
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Oct 2006
Posts: 88
Reputation: dami06 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dami06 dami06 is offline Offline
Junior Poster in Training

Re: Storing questions in the database

  #7  
Feb 13th, 2008
ok..thank you, i'll try and see how far i can get on with this
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is online now Online
Posting Sensei

Re: Storing questions in the database

  #8  
Feb 13th, 2008
You are welcome! Its easy. Just keep trying.. You ll get it. Best of luck!
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC