Forum Moderators: coopster

Message Too Old, No Replies

Displaying MySQL records with radio buttons

Please Help :)

         

Ellason

6:43 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



Hello, I was wondering if anyone can have a look at my code and see whats wrong with it. I am creating a MySQL/PHP quiz system where there is an admin form where a user can add a question ID, Question, and for possible answers which are represented as radio buttons in a html form. The correct answer is the selected radio button beside one of the possible answers. The correct answer is stored in the dtabase along with these other fields. I have now created a html form with a button that when clicked runs a PHP script that will retrieve the Question and possible answers from the database. I want each possible answer to be displayed with a radio button so it can then be used as a quiz and the results stored in a separate database table. My code is shown below. Although it is producing an error with this line:

echo "<input type = radio name = Option value ='".$row->Option2"'</input>";

Can anyone explain why?
Thanks in advance.

#!c:/php/php.exe
<?php

// open database connection
$server="localhost";
$user="student1";
$password="student1pw";
$database="localdatabase";

$connect = mysql_connect("$server","$user","$password");
if (!$connect)
{
die('Could not connect!' . mysql_error());

// generate and execute query
$query = "SELECT QuestionID, Question, Option1, Option2, Option3, Option4 FROM compwork";
$result = mysql_query($query) or die("ERROR: $query.".mysql_error());

// if records are present
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_object($result);


if (mysql_num_rows($result) > 0) {

// print answer list as radio buttons
while ($row = mysql_fetch_object($result)) {
echo "<input type = radio name = Option value ='".$row->Option1"'</input>";
echo "<input type = radio name = Option value ='".$row->Option2"'</input>";
echo "<input type = radio name = Option value ='".$row->Option3"'</input>";
echo "<input type = radio name = Option value ='".$row->Option4"'</input>";
}


echo "<input type = submit name = submit value = 'Submit!'>";
}

echo '</form>';
}

if (!mysql_query($sql,$connect))
{
die ('Error' . mysql_error());
}

}
mysql_close($connect)
?>

Mahabub

7:06 pm on Feb 12, 2009 (gmt 0)

10+ Year Member



Dear Ellason,

I just saw your code and find some syntax error on the below lines.

echo "<input type = radio name = Option value ='".$row->Option1"'</input>";
echo "<input type = radio name = Option value ='".$row->Option2"'</input>";
echo "<input type = radio name = Option value ='".$row->Option3"'</input>";
echo "<input type = radio name = Option value ='".$row->Option4"'</input>";

It should be like:

echo "<input type = radio name = Option value ='".$row->Option1."'</input>";
echo "<input type = radio name = Option value ='".$row->Option2."'</input>";
echo "<input type = radio name = Option value ='".$row->Option3."'</input>";
echo "<input type = radio name = Option value ='".$row->Option4."'</input>";

Please let us know actually what error you have got.

Thanks
Mahabub

andrewsmd

7:50 pm on Feb 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just an fyi it makes your code easier to read if you write it like this.
echo"<input type = radio name = Option value ='{$row->Option1}'</input>";

rocknbil

9:22 pm on Feb 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



</input>

I may have missed the memo, why are you using a (invalid? nonexistent?) closing tag on a radio? Honestly I've never seen this.

<input type="radio" name="Option" id="Option1234" value="somevalue">

echo "<input type=\"radio\" name=\"Option\" id="\Option" . $row->Option2 . "\" value=\"". $row->Option2 . "\">";

If you're using an XHTML doctype (which you only should if you're actually using XHTML features,)

echo "<input type=\"radio\" name=\"Option\" id="\Option" . $row->Option2 . "\" value=\"". $row->Option2 . "\"/>";

As for the checked value,

echo "<input type=\"radio\" name=\"Option\" id="\Option" . $row->Option2 . "\" value=\"". $row->Option2 . "\"";

if (isset($row->Option2) and ($row->Option2 != '')) { echo ' checked'; }
echo ">";

Although, by nature of the way a radio button is supposed to work, one of the values should always be checked by default. So it might be better to rethink how you're accessing these, and it would simplify your overall problem. I suggest getting all the options into an array, if none of them have a value assign "checked" to the default one. Then output the radio buttons.

If it's vital that you allow none of them to be checked, you should be using a checkbox.

Ellason

2:00 pm on Feb 16, 2009 (gmt 0)

10+ Year Member



Hey, thanks for the replys. I tried each of the solutions and the solution provided by andrewsmd and rocknbil did not work, the error I am getting is:

Parse error: parse error, expecting `','' or `';'' in C:\Apache\Apache2\cgi-bin\RadioButtons.php on line 27

I tried the solution provided by Mahabub and it does not produce this error, it just produces a blank screen, can anyone suggest why this is?

Thanks again

rocknbil

7:03 pm on Feb 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



PHP Error reporting is turned off by default on many systems as it often reveals too much about the system. Depending on when the error occurs, you can get some of them to print to the browser with this at the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', '1');

Per the error in your logs, it's (probably) right where it's always been as Mahabub pointed out:

.....value ='" . $row->Option1"'</input>";

.....value ='" . $row->Option1 <where is the dot?> "'</input>";

You're missing concatenation operators (.) Look right after "Option1". When concatenating it's easier to "spot" the missing ones if you add spaces:

....value ='" . $row->Option1 . "'</input>";

It's still an invalid input tag though, unless I'm missing something?

Ellason

1:08 pm on Feb 20, 2009 (gmt 0)

10+ Year Member



Hey guys, thanks for all yout replies :D I have got it working although I'm stuck on the next function I have to create.
At the minute when a user clicks on a quiz button it retrieves 5 random questions from the database and displays then on screen with the 4 possible answers and a submit button. This means the user can then take this quiz. How do i then save the answers they give in the database once they have clicked the submit button. Because the quiz is presented in a php page how do I write another php script and link it to this page? Sorry if this sounds confusing. Any ideas?
Thanks

andrewsmd

1:17 pm on Feb 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is a few ways. On the original page you can use
<?php
require_once("nameOfYourScript.php");
?>
Then in nameOfYourScript.php you can treat it as if the php were in the original file. The other way would be in your form tag
<form name = "form" method = "post" action = "nameOfYourScript.php">
then it will get executed when you click submit. However if you have multiple submit buttons you will always execute that script on a submit click

Ellason

1:40 pm on Feb 20, 2009 (gmt 0)

10+ Year Member



Great thanks, I thought it might have been something like that. I'll give it a go and see what happens. Thanks so much!

Ellason

1:23 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



Hey thanks for all the help. I am now stuck on the next part. I currently have a php script that runs and displays 5 random questions from a database with the four possible answers as radio buttons. The student can now use this as a quiz and when they have selected their answers they click the submit button. I now want to write a script that will compare the selected answers with the the saved answers in the database. any ideas on how i'd go about doing this?

Thank u

rocknbil

4:54 pm on Mar 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Table answers
id¦question_id¦sequence¦correct answer¦answer_text

Table students
id¦question_id¦answer

(correct answer is blank or 1, question_id, sequence, and answer are integer fields.)

$out_text = NULL;

$select = "select answers.sequence, answers.correct_answer, answers.answer_text, students.answer, from answers,students where answers.question_id=students.question_id and answers.correct_answer<>'' order by answers.sequence asc";


$result=@mysql_query("$select");
if (!$result) { error("Cannot get answers:" . mysql_error()); }
while ($row=mysql_fetch_array($result)) {
list($num,$correct_ans,$ans_text,$student_ans) = $row;
if ($student_ans==$correct_ans) {
$out_text = "<li>Question $num, Correct: $ans_text</li>\n";
}
else {
$out_text = "<li>Question $num, Incorrect, the correct answer is $ans_text</li>\n";
}
}
mysql_free_result($result);

header("content-type:text/html\n\n");
print "<h1>Results</h1>\n";
print "
<ul>
$out_text
</ul>
";