473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to submit multiple values to the database with a single submit button?

127 100+
i have questions and answers for users to submit.
i have stored questions in questions table. qid, qtext.
answers in answers table. aid,atext,cans (correct ans)

i have displayed the questions and multiple answers from database. like this..

What is capital of India
1.Chennai
2.Bangalore
3.Delhi

What is capital of England
1.London
2.Paris
3.Madrid

now the user can select one answer for each question..

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $sql="SELECT * FROM questions, answers WHERE answers.qid = questions.qid";
  3. $result=mysql_query($sql);
  4.  
  5. while ($row = mysql_fetch_array($result)) {
  6.   if ($row['qtext'] != $lastQuestion) {
  7.     echo "<p>" . $row['qtext'] . "</p><br />";
  8.     $lastQuestion = $row['qtext'];
  9.           }
  10.   echo "<input type='radio' name='".$row['qid']."' value='".$row['aid']."' />".$row['atext'];
  11.  
  12.   }
  13.  
  14. ?>
  15.  
  16.  
after answering the question clicks the submit button. how can i store the users answers in the database..??
May 14 '13 #1
14 7876
Rabbit
12,516 Expert Mod 8TB
Well, they would fill out the form by selecting their answers. They would then submit their form to another page. That page would use INSERT statements to save their selections to the database.
May 14 '13 #2
impin
127 100+
when user clicks the submit button, in the next page how can i retrieve the values to store in the database?
how can i pass the values in the submit button to the next page? there are 3 questions and 3 answers...
for each user i have to to store question id and his ans id in the database? how can i do it.
May 15 '13 #3
Rabbit
12,516 Expert Mod 8TB
That depends on how you're passing the form. If you're using POST then you use the $_POST array to retrieve the values submitted. If you're using GET, then you use the $_GET array.
May 15 '13 #4
impin
127 100+
How can add the php code inside the html form. after the user selecting the answers, he clicks the submit button the page must go to checkresult.php.

Expand|Select|Wrap|Line Numbers
  1. <form id="questionBox" method="post" action="checkresult.php" >
  2. <input type="submit" id="submit" name="submit" value="Submit Answer" /></p>
  3. </form>
Expand|Select|Wrap|Line Numbers
  1. $sql="SELECT * FROM questions";
  2. $result=mysql_query($sql);
  3. while ($row = mysql_fetch_array($result)) {
  4. echo "<p>" . $row['qtext'] . "</p>";
  5. $sql2="SELECT * FROM answers where qid=".$row['qid'];
  6. $result2=mysql_query($sql2);
  7. while($row2=mysql_fetch_assoc($result2)){
  8. echo "<input type='radio' name='".$row['qid']."' value='".$row2['cans']."' />".$row2['atext']; }
  9. }
where should i add the submit button in the php code???
May 16 '13 #5
impin
127 100+
This is the form.
how to pass the values to the checkresultpage.php and how to retrieve the values in the checkresultpage.php?

Expand|Select|Wrap|Line Numbers
  1. echo "<form method='post' id='submit' action='checkresult.php'>";
  2. $sql="SELECT * FROM cquestions";
  3. $result=mysql_query($sql);
  4. while ($row = mysql_fetch_array($result)) {
  5. echo "<p>" . $row['cqtext'] . "</p>";
  6. $sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
  7. $result2=mysql_query($sql2);
  8. while($row2=mysql_fetch_assoc($result2)){
  9. echo "<input type='radio' name='".$row['cqid']."' value='".$row2['ccans']."' />".$row2['aatext']; }
  10. /*echo "<input type='hidden' name='email' value='email' />";*/
  11. }
  12. echo"<input type='submit' id='submit' name='submit' value='Submit Answers' />";
  13. echo "</form>" ?>
May 16 '13 #6
Rabbit
12,516 Expert Mod 8TB
You already have your submit button in there so when they click that it will submit their answers. So I don't know why you're asking how to submit it, you already have the code in there to do that.

As for retrieving the values submitted, I answered that already in post #4. Since you're using post to submit your data, you will use the $_POST array to retrieve your data.
May 16 '13 #7
impin
127 100+
okay. but i am not sure the above code is working or not? when i click the submit button it goes to checkresult.php, but nothing happens...

checkresults.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("config.php");
  3. include("lock.php");
  4.  
  5. $login_session = $_POST['email'];
  6. /*$aaid =$_POST['aaid'];*/
  7.  
  8.  
  9. $login_session=$_POST['email'];
  10. $marks=0;
  11. $sql="SELECT * FROM cquestions";
  12. $result=mysql_query($sql);
  13. while ($row = mysql_fetch_array($result)) {
  14. $qid=$row['cqid'];
  15. $marks+=$_POST['$cqid'];
  16. }
  17. $insert="insert into result(email,marks)values('$email',$marks)";
  18. mysql_query($insert);
  19. ?>
  20.  
May 17 '13 #8
Rabbit
12,516 Expert Mod 8TB
That's because you have a few errors in your php that you have only now shown us for the first time.

1. You have no submitted variable named email because you commented it out in the page prior.

2. You have no variable $cqid, it's $qid.

3. You have no variable $email, it's $login_session.

You really need to double check your variables and be consistent. You use one variable name in one place and then you use a completely different variable name in another.

On a side note, you have left yourself open to injection attacks. At the least you should escape your SQL.
May 17 '13 #9
impin
127 100+
these are the tables i used.
Database Tables

CQuestions
CQid, CQText
CAnswers
Aaid, AaText, CQid, CQAns
CResults
Rid, Email, Marks

test.php

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. include("config.php");
  3. session_start();
  4. include("lock.php");
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  10. <link rel="stylesheet" href="style1.css" type="text/css" />
  11. <title>QContest</title>
  12. </head>
  13. <body>
  14. <div id="wrapper">
  15. <div id="intro">
  16. <h1>Take the test </h1>
  17. <p>Each Questions 4 possible answers. Choose the answer you think is correct and click <strong>'Submit Answer'</strong>.
  18. </div><!--intro-->
  19. <div id="quiz">
  20. <h2>Questions : <?php echo $login_session;?> </h2>
  21. <?php
  22. echo "<form method='post' id='submit' action='checkresult.php'>";
  23. $sql="SELECT * FROM cquestions";
  24. $result=mysql_query($sql);
  25. while ($row = mysql_fetch_array($result)) {
  26. echo "<p>" . $row['cqtext'] . "</p>";
  27. $sql2="SELECT * FROM canswers where cqid=".$row['cqid'];
  28. $result2=mysql_query($sql2);
  29. while($row2=mysql_fetch_assoc($result2)){
  30. echo "<input type='radio' name='".$row['cqid']."' value='".$row2['ccans']."' />".$row2['aatext']; }
  31. /*echo "<input type='hidden' name='email' value='email' />";*/
  32. }
  33. echo"<input type='submit' id='submit' name='submit' value='Submit Answers' />";
  34. echo "</form>" ?>
  35.  
test.php screen shot



checkresult.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. include("config.php");
  3. include("lock.php");
  4.  
  5. $login_session = $_POST['email'];
  6. /*$aaid =$_POST['aaid'];*/
  7.  
  8.  
  9. $login_session=$_POST['email'];
  10. $marks=0;
  11. $sql="SELECT * FROM cquestions";
  12. $result=mysql_query($sql);
  13. while ($row = mysql_fetch_array($result)) {
  14. $qid=$row['cqid'];
  15. $marks+=$_POST['$cqid'];
  16. }
  17. $insert="insert into result(email,marks)values('$email',$marks)";
  18. mysql_query($insert);
  19. ?>
how to pass the user's answers to checkresult.php page and store the marks in database?
May 17 '13 #10
Rabbit
12,516 Expert Mod 8TB
You just repeated what you said before. I've already answered the questions that you just asked.
May 17 '13 #11
impin
127 100+
just tell me whether the test.php code is correct or not? is there any mistake in the test.php? please tell what are the mistakes in test.php?
May 18 '13 #12
Rabbit
12,516 Expert Mod 8TB
test.php looks fine. It's checkresult.php that looks wrong. And I already told you what's wrong with it above.
May 18 '13 #13
impin
127 100+
okay. thank you. i fixed it.
May 19 '13 #14
Rabbit
12,516 Expert Mod 8TB
Glad you solved your problem. Can you post your solution in case someone else runs into the same problem?
May 19 '13 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Emmett Power | last post by:
Hi, I have a form on a web page with a number of radio buttons bound to the same field. Is it possible to set up the form so that users can select more than one radio button to submit multiple...
2
by: Jen F. | last post by:
I have inherited a medical database in which there are multiple values stored in a single field (ie. "Current Conditions" field might contain 1-20 different conditions, separated by comma (ie....
4
by: NAJH | last post by:
Taking the Northwind database as an example, I have an order table: 'Orders' a order details table: 'Order Details' and a products table: 'Products' For reasons best ignored, I want to produce...
9
bhing
by: bhing | last post by:
Hi, please help me. I have a few forms and I need send there forms by one submit input. They have the same form action and almost the same fields. It is like a referral page. for example ...
1
jenkinsloveschicken
by: jenkinsloveschicken | last post by:
Is it possible to compare a single value to many in a single statement? Here is what I am attempting to do: 1. Users lands on page and via a cookie check function they are identified. 2. A query...
2
by: thomasrye | last post by:
I'm not extremely well versed in MySQL... this may end up being a fundamental or basic in database design and implementation. (by the way I'm using PHP) I'll give the trimmed down example of what...
17
by: msmjsuarez | last post by:
Hello, I need help... How to automatically insert data to mysql database without submit button using php? Is there a way on this? thanks a lot.
4
by: velocity00723 | last post by:
I am trying to login for website with autologin to forum,and vcalender its work fine in mozilla and ie but it gives problem in safari(IN MAC) following is the code if(isset($_POST)) {...
1
by: chintan85 | last post by:
Hi guys, I want to pass multiple forms to different scripts. Lets say. Can u please help me.. <form action="pass1.py" method="post" name="Submit"> <input name="gene" type="checkbox"...
4
by: Akpo | last post by:
Hi all...i am working on a project to capture students' data for an educational institution. I want to have a form that allows multiple student records to be filled in at once..e.g(this is the form):...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.