html - Getting Values / Answers from radio button and storing it in SQL Database using PHP -
i doing online aptitude test company pick 20 random questions database , display on webpage answering.
but have problem in storing answered values in sql database, please 1 can me issue,
<?php $connect = mysql_connect("localhost","root","") or die(mysql_error()); $sel=mysql_select_db("aptitude"); $query = mysql_query("select * `questions` order rand() limit 20 "); while($rows = mysql_fetch_array($query)){ $q = $rows['qno']; $qus = $rows['question']; $a = $rows['opt1']; $b = $rows['opt2']; $c = $rows['opt3']; $d = $rows['opt4']; $ans = $rows['ans']; echo "<b>question:-<br></b>$qus <br>"; echo " <input type=radio name = 'answer[$q]' value = '$a'></input>$a    "; echo " <input type=radio name = 'answer[$q]' value = '$b'></input>$b    "; echo " <input type=radio name = 'answer[$q]' value = '$c'></input>$c     "; echo " <input type=radio name = 'answer[$q]' value = '$d'></input>$d <br><br> "; } ?>
but trying store values after user clicking on submit button:
if (isset($_post['submit'])) { $username=$_get['username']; $opt1=$_post["answer1"]; $opt2=$_post["answer2"]; mysql_query("insert $username values('q1','$answer1')") or die(mysql_error()); mysql_query("insert $username values('q2','$answer2')") or die(mysql_error()); }
first of should create form , print question , answer related data in form , after submission of form should required data that's have set in form.
in case there no need to store answer value in variable @ form. have create 1 submit handler in selected option , question id. store data in database. , if want create result sheet create function compare user submitted answer database answer.
submit handler goes this
if (isset($_post['submit'])) { if (isset($_post['username']) && $_post['username']){ $username = $_post['username']; } if (isset($_post['qno']) && $_post['qno']){ $qno = $_post['qno']; } if (isset($_post['answer']) && $_post['answer']){ $answer = $_post['answer']; } mysql_query("insert 'your_table_name' values('values1','value2')")or die(mysql_error());}
if need more provide email id ill give full module.
Comments
Post a Comment