how to store values in array php - php

This is my code to display list of questions and 4 options( using option box). user has to select the correct option . Is it possible to store the user selected options in array variable. There may be 10 to 20 questions array variable should contain only option values.
<?php
require('connect.php');
$display=mysql_query("SELECT * FROM Java_Test_1 ORDER BY id ASC");
echo "<form method=post action=$PHP_SELF>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display))
{
$id=$row['0'];
$question=$row['1'];
$opta=$row['2'];
$optb=$row['3'];
$optc=$row['4'];
$optd=$row['5'];
$answer = $row["woptcode"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opta<input type=radio name=$id value='a'></input></td><td>$optb <input type=radio name=$id value='b'></input></td><td>$optc <input type=radio name=$id value='c'> </input></td><td>$optd <input type=radio name=$id value='d'></input></td></tr>";
}
echo "</table>";
echo "<input type='submit' value='start' name='doo'>";
echo "</form>";
}

$name = 'opt';
$counter = 0;
while ($row = mysql_fetch_array($display))
{
$name = 'opt'.$counter;
$counter++;
$id=$row['0'];
$question=$row['1'];
$opta=$row['2'];
$optb=$row['3'];
$optc=$row['4'];
$optd=$row['5'];
$answer = $row["woptcode"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opta<input type=radio name=$name value='a'></td>
<td>$optb <input type=radio name=$name value='b'></td>
<td>$optc <input type=radio name=$name value='c'></td>
<td>$optd <input type=radio name=$name value='d'></td>
</tr>";
}

Here is the official documentation that expains this in detail
http://php.net/manual/en/faq.html.php
Section: How do I create arrays in a HTML ?

Related

My PHP code returns Array to string conversion error on trying to display values of the radio buttons

This is the PHP code to display some content and corresponding 5 radio buttons.
<form name="functional" method="post" action="funcsub.php">
<?php
$n=0;
$con=mysqli_connect('localhost','Sanjana','sanjana');
mysqli_select_db($con,'mydatabase');
$sql = mysqli_query($con,"select * from skills") or die("Failed");
while($result = mysqli_fetch_array($sql)){
?>
<br/>
<?php
echo $result["Technical"];
echo "</br>";
echo"<input type='radio' name='tech[$n]' value='0' required>0";
echo"<input type='radio' name='tech[$n]' value='1'>1";
echo"<input type='radio' name='tech[$n]' value='2'>2";
echo"<input type='radio' name='tech[$n]' value='3'>3";
echo"<input type='radio' name='tech[$n]' value='4'>4";
echo"<input type='radio' name='tech[$n]' value='5'>5";
$n=$n+1;
}
?>
<br/>
<input type="submit" name="submit" value="Submit">
</form>
</html>
The below code is to get the values of the radio buttons and display it corresponding to the data.
<?php
$con=mysqli_connect('localhost','Sanjana','sanjana');
mysqli_select_db($con,'mydatabase');
$sql = mysqli_query($con,"select * from skills") or die("Failed");
while($result=mysqli_fetch_array($sql))
{
$skill = $result["Technical"];
echo $skill;
$op_num = $_POST['tech'];
echo $op_num;
echo "<br/>";
}
?>
It returns an error stating Array to String Conversion. Any idea on how to resolve this??

how to select multiple radio button in same while loop which is echoed from db

guyz, i want to select radio button for each question.please help me to do that.now i can select only one radio button in a page...... check this link: http://i.stack.imgur.com/UI2vp.png
while($row= mysql_fetch_row($rs)){
echo "<form name=myfm method=post action=Quiz.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$n+1;
echo "<tr><td><span class=style2>Question ". $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name='ques[$n][]' value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name='ques[$n][]' value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name='ques[$n][]' value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name='ques[$n][]' value=4>$row[6]";
}
How to fetch the data and store it to mydb...
$query="select * from question";
$rs=mysql_query("select * from question where testid=$tid",$cn) or die(mysql_error());
if($submit=='Get Result')
{
$n=0;
while($row= mysql_fetch_row($rs)){
for($i=0;$i<count($_POST['ques']);$i++)
{
$ans=$_POST['ques'][$n][$i];
echo $ans;
$n=$n+1;
mysql_query("insert into useranswer(sessid, testid, ques, ans1,ans2,ans3,ans4,correctans,yourans) values ('".session_id()."', $tid,'$row[2]','$row[3]','$row[4]','$row[5]', '$row[6]','$row[7]','$ans')") or die(mysql_error());}
}
}
is this works???....
Just replace ques[] with ques[". $n ."][] like this..
while($row= mysql_fetch_row($rs)){
echo "<form name=myfm method=post action=Quiz.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$n+1;
echo "<tr><td><span class=style2>Question ". $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name='ques[".$n."][]' value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name='ques[". $n ."][]' value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name='ques[". $n ."][]' value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name='ques[". $n ."][]' value=4>$row[6]";
}
You have to name your radiobuttons for each set.
while($row=mysql_fetch_row($rs)) {
echo "<form name=myfm method=post action=Quiz.php>";
echo "<table width=100%> <tr> <td width=30> <td> <table border=0>";
$n=$n+1;
echo "<tr><td><span class=style2>Question ". $n .": $row[2]</style>";
echo "<tr><td class=style8><input type=radio name='ques".$n."' value=1>$row[3]";
echo "<tr><td class=style8> <input type=radio name='ques".$n."' value=2>$row[4]";
echo "<tr><td class=style8><input type=radio name='ques".$n."' value=3>$row[5]";
echo "<tr><td class=style8><input type=radio name='ques".$n."' value=4>$row[6]";
echo "</table></table></form>"; //AND you should close your tags
}
Also, you should refrain from using mysql_functions, which are deprecated and removed in PHP7.
The mistake is that you are naming all your radio buttons with the same name. That makes all of them in one group. Name each group with three different names. Your problem is solved

How do I update each row individually?

So I've been trying for days to figure out how I can update each row separately and i can't figure it out. this is the first time I've used php so sorry for the ugly code.
I can seem to pull the information from each row but the update wont work, however it sends all the data from all the rows in the address bar
<html>
<head>
<title>EDIT Patient data</title>
</head>
<body>
<br>
Edit Patients<br>
Patient Request<br>
Booked Patients
<?php
include 'connect.php';
$sql = "SELECT * FROM patients;";
$result = mysql_query($sql);
$num_of_row = mysql_num_rows($result);
if (isset($_GET['submit'])) {
$id = $_GET['did'];
$fname = $_GET['dfname'];
$lname = $_GET['dlname'];
$email = $_GET['demail'];
$phone = $_GET['dphone'];
$address = $_GET['daddress'];
$query = mysql_query("update patients set fName='$fname', lName='$lname', email='email', phone='$phone', address='$address' where patientID ='$id'");
}
echo "<table border=1>";
echo " <tr> <td> Patient ID </td> ";
echo "<td> First name </td>";
echo "<td> last name </td>";
echo "<td> email </td>";
echo "<td> phone number</td>";
echo "<td> address </td>";
echo "<td> date of birth </td> ";
echo "<td> update </td></tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr> <form class='form' method='get'>";
echo "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
echo "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
echo "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
echo "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
echo "<td> <input type='text' name='dphone' value ='".$row['phone']."' /></td>";
echo "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
echo "<td> <input type='text' name='ddob' value ='".$row['dob']."' readonly /></td>";
echo "<td> <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
?>
</body>
</html>
Does your user have update permissions?
If not you need to use GRANT UPDATE ON *.* TO 'User'#'Host';
But you should use MySQLi as MySQL is deprecated. You should also look into Parameterized Queries and MySQL injection, It's worth knowing about!
If you want to update an individual row, you'll have to put a <form> around your while-loop, like this:
echo "<form action='' method='GET'>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr> <form class='form' method='get'>";
echo "<td> <input type='text' name='did' value='".$row['patientID']."' readonly /></td>";
echo "<td> <input type='text' name='dfname' value ='".$row['fName']."' /></td>";
echo "<td> <input type='text' name='dlname' value = '".$row['lName']."' /></td>";
echo "<td> <input type='text' name='demail' value = '".$row['email']."' /></td>";
echo "<td> <input type='text' name='dphone' value = '".$row['phone']."' /></td>";
echo "<td> <input type='text' name='daddress' value ='".$row['address']."' /></td>";
echo "<td> <input type='text' name='ddob' value ='".$row['dob']."' readonly /></td>";
echo "<td> <input class='submit' type='submit' name='submit' value='update' /> </td> </tr>";
}
echo "</form>";
The form will allow input fields to be submit according to the form's method (GET and POST).

how to show the correct result of the quiz

I m trying to make a quiz.It's working but not giving the right result. On a correct answer for example answer 1 variable rans should be incremented by one but it is incrementing after submitting the 2nd question, that's why the value of the 10th current answer is not including in the total correct answer.
<?php
require_once("global.inc.php");?>
<form name="test" method="post" action="test.php">
<?php
$qid=(!isset($_POST['q_id'])) ? 0 : $_POST['q_id'];
$rans=(!isset($_POST["rans"])) ? 0 : $_POST["rans"];
$totalquestion=(!isset($_POST["totalquestion"])) ?
0 : $_POST["totalquestion"];
echo $rans;
if(isset($_POST["submit"]))
{
echo "<table align='center' style='border:1px solid silver' width='80%'
bgcolor='green'>";
echo "<tr><td>Total Question Attempt</td><td>",$totalquestion,"</td><tr>";
echo "<tr><td>Correct Answer</td><td>",$rans,"</td></tr>";
echo "<tr><td>Wrong Answer</td><td>",$totalquestion-$rans,"</td></tr>";
echo "<tr><td>Correct Answer Percentage</td> <td>",$rans/$totalquestion*100,"%</td></tr>";
echo "<tr><td>Wrong Answer Percenntage</td><td>",($totalquestion-$rans)/$totalquestion*100,"%</td></tr>";
echo "</table><br><br>";
$query="select * from questions,answers
where questions.q_id=answers.q_id";
echo "<table cellpadding='5px' align='center' style='border:1px
solid silver'>";
echo "<tr><th colspan='4' id='heading'>Online Quiz Test
Question</td></tr>";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>",$row['q_id'],"</td><td colspan='2'>",$row['question'],"</td></tr><tr><td></td>";
echo "<td colspan='2'>A. ",$row['opt1'],"</td>";
echo "<td colspan='2'>B. ",$row['opt2'],"</td></tr>";
echo "<tr><td></td><td colspan='2'>C. ",$row['opt3'],"</td>";
echo "<td colspan='1'>D. ",$row['opt4'],"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'>Correct option is ",strtoupper($row['correct_ans']),"</td></tr>";
echo "<tr><td colspan='4' align='right'
style='color:orange'><hr></td></tr>";
}
echo "</table>";
echo "<p align='right'><a href='#' onclick='window.print()'>Print</a></p>";
echo "<div style='visibility:hidden;display:none'>";
}
?>
<form name="test" method="post" action="test.php">
<?php
if(!isset($a))
{
$a=0;
//unset($_SESSION['score']);
}
if(isset($_POST['next'])) {
$a=$_POST['a'];
$totalquestion=$_POST['totalquestion'];
if(isset($_POST['rans']))
$rans=$_POST['rans'];
}
$sql1="SELECT * FROM questions,answers
where questions.q_id=answers.q_id limit 1 offset $a";
$result=mysql_query($sql1);
$num = mysql_num_rows($result);
echo "<form method='post' action=''>";
if($result) {
while ($row = mysql_fetch_array($result))
{
$qid = $row["q_id"];
$questions = $row["question"];
$opt1 = $row["opt1"];
$opt2 = $row["opt2"];
$opt3 = $row["opt3"];
$opt4 = $row["opt4"];
$correct = $row["correct_ans"];
echo $rans;
?>
<p >Q.<?php echo $qid ?> <?php echo $questions;?></p>
<input type="radio" value="<?php echo $opt1;?>" name="choice"/><?php echo $opt1;?> <br/>
<input type="radio" value="<?php echo $opt2;?>" name="choice"/><?php echo $opt2;?><br/>
<input type="radio" value="<?php echo $opt3;?>" name="choice"/><?php echo $opt3;?><br/>
<input type="radio" value="<?php echo $opt4;?>" name="choice"/><?php echo $opt4;?><br/>
<input type="hidden" value="$answer" name="rightanswer[$qid]"/>
<?php
$b=$a+1;
$sql2="SELECT * FROM questions where q_id=$qid-1 ";
$result2=mysql_query($sql2);
while ($row2 = mysql_fetch_array($result2)) {
$ans=$row2['correct_ans'];
}
if(isset($_POST['choice'])) {
if($ans==$_POST['choice']){
//echo "<input type='hidden' name='rans' value='".($rans+1). "'>";
$rans=$rans+1;
}
else {
//echo "<input type='hidden' name='rans' value='" . $rans . "'>";
$rans=$rans;
}
}
//$query="select correct_ans from questions where q_id='$qid'";
//$result=mysql_query($query);
//while ($row = mysql_fetch_array($result)) {
//echo $row['correct_ans'];
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='hidden' value='count' name='count'>";
echo "<input type='hidden' name=qid value='$qid'>";
echo "<input type='hidden' name='totalquestion' value='".$totalquestion+1)."'>";
echo "<input type='hidden' name='rans' value='" . $rans . "'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='submit' name='submit' value='submit'><br><br>";
echo "</form>";
}
}
?>
Okay, your code is a bit of a mess.
You have random tautologies (like the $rans=$rans; which does absolutely nothing. If the answerer clicks "next" you're assigning $totalquestion twice. Definitely take a good, hard look and refactor this page.
But the answer to your question is probably because you're checking to see if they entered in the right answer at the bottom of the code -- after you've presented the results or the next question.
You've utilized the scripting capabilities of PHP without touching on any functions so it will evaluate top to bottom.
I'd move everything around: Move the handler for "next" to the top, underneath your default variable assignments, then put the check for right answer underneath that, then do the presentation of the next question, then the "submit"handler.
I'd break up the different units into functions for readability and reusability, also. For example, make a function to print out the specified question, make another one to validate the user entered in the right answer.

online quiz using php and mysql(radio button)

please help, i'm developing an online quiz application. All the questions and answers will be selected from the database. Where i'm having probkem with is to get the values from the radio button whether checked or not. bellow is the code that generate the questions and answers from database.
if (!isset($_POST['submit'])) {
echo "<form method=post action='#'>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$opt5 = $row["ans5"];
$answer = $row["ans"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td>$opt1 <input type=radio name=q$id value=\"$opt1\"></td><td>$opt2 <input type=radio name=q$id value=\"$opt2\"></td><td>$opt3 <input type=radio name=q$id value=\"$opt3\"></td><td>$opt4 <input type=radio name=q$id value=\"$opt4\"></td><td>$opt5 <input type=radio name=q$id value=\"$opt5\">q$id</td></tr>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
the name of the radio button is
<input type='radio' name='q$id' value='$opt4' />
How do i get the value of the checked radio button?
or is my PHP code wrong?
what i needed is to output what is selected if a radio button is checked.
if(isset($_POST['submit']))
{
$value = $_POST[''];//the value of the radio button, i don't know what to put here
$n = count($value);
for($i=0; $i < $n; $i++)
{
echo $value[$i];
}
}
Try:
if(isset($_POST['submit']))
{
$value = $_POST;//the value of the radio button, i don't know what to put here
$n = count($value);
for($i=0; $i < $n; $i++)
{
echo $value[$i];
}
}
$_POST[''] was wrong. You needed to use just $_POST.
You could also just use foreach here:
if(isset($_POST['submit']))
{
$values = $_POST;//the value of the radio button, i don't know what to put here
foreach($values as $value)
{
echo $value;
}
}
$_POST is just a array containing whatever your form submited. If you had a name textfield you would use:
echo $_POST['name'];
to echo it.
Try
var_dump("<PRE>", $_POST);
And you will see exactly how your form is being organised. Link to documentation
thanks for your response. i was able to come out with this code and it works but i don't know if the structure or the way i solved it is proper or good enough.
<?php
if (!isset($_POST['submit'])) {
echo "<form method=post action='#'>";
echo "<table border=0>";
while ($row = mysql_fetch_array($display)) {
$id = $row["id"];
$question = $row["question"];
$opt1 = $row["ans1"];
$opt2 = $row["ans2"];
$opt3 = $row["ans3"];
$opt4 = $row["ans4"];
$opt5 = $row["ans5"];
$answer = $row["ans"];
echo "<tr><td colspan=3><br><b>$question</b></td></tr>";
echo "<tr><td><input type=radio name=$id value=\"$id $opt1\">$opt1 </td><td><input type=radio name=$id value=\"$id $opt2\">$opt2 </td><td><input type=radio name=$id value=\"$id $opt3\">$opt3 </td><td><input type=radio name=$id value=\"$id $opt4\">$opt4 </td><td><input type=radio name=$id value=\"$id $opt5\">$opt5 <br>";
}
echo "</table>";
echo "<input type='submit' value='See how you did' name='submit'>";
echo "</form>";
}
?>
what i did was to include the id of the question in the value of the radio button
<input type=radio name=$id value=\"$id $opt1\">
i collect the values of the radio button using $_POST as suggested, i then explode the value into two arrays so the first array will contain the question id and the second will contain the actual value of the radio button (answer).
<?php
elseif (isset($_POST['submit']))
{
var_dump("<PRE>", $_POST);
$sd = $_POST;
foreach($sd as $sd)
{
$tok = explode(" ", $sd, 2);
$w = mysql_query("select * from `questions` WHERE `id` = '$tok[0]'")or die(mysql_error());
if(mysql_num_rows($w) == 1)
{
$sf = mysql_fetch_object($w);
echo $tok[1].".......".$sf->ans."<br>";
}
}
}
?>
it works perfectly but will it give efficient result?

Categories