Ellason Posted February 10, 2009 Share Posted February 10, 2009 Hello. I have created an admin form for a PHP/MySQL quiz system I am creating. Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html> <body> <form action= "/cgi-bin/SoftwareQuiz.php" method="post"> <h3>Adding a question to the "Computer Software" Unit:</h3> <h5>Question ID's must be 4 digits long, Begin each Question ID with 300 and add a number at the end to make each question unique. Start with the number 1.</h5> <tr><td>QuestionID:</td><td><textarea cols='10' name="QuestionID" ></textarea></td></tr><br><br> <tr><td>Question:</td><td><textarea cols='40' name="Question" ></textarea></td></tr><br><br> <tr><td>Option1:</td><td><tr><td align="left"><input type="radio" name="Answer" value="1"/></td><td><input type="text" name="o1"/></td></tr><br><br> <tr><td>Option2:</td><td><tr><td align="left"><input type="radio" name="Answer" value="2"/></td><td><input type="text" name="o2"/></td></tr><br><br> <tr><td>Option3:</td><td><tr><td align="left"><input type="radio" name="Answer" value="3"/></td><td><input type="text" name="o3"/></td></tr><br><br> <tr><td>Option4:</td><td><tr><td :align="left"><input type="radio" name="Answer" value="4"/></td><td><input type="text" name="o4"/></td></tr><br><br> <tr><td align="left" colspan='2'><input type="submit" name="submit" value="Add Question"/></td></tr><br><br> </body> </html> It basically allows a user to add a Question, Question ID, and four possible answers. The correct answer is the radio button that is selected. The QuestionID, Question, four possible answers and the correct answer is stored in the database. I now want to create a html form that has a button that when clicked will run a php script that retrieves random questions from the database and display the question along with the four possible answers. Although I want the possible answers to be displayed with radio buttons so it can be used as a quiz and the answers can be stored. Can anyone advise me how to do this?? Thanks Link to comment https://forums.phpfreaks.com/topic/144629-retrieving-mysql-records-and-display-with-radio-buttons/ Share on other sites More sharing options...
Ellason Posted February 10, 2009 Author Share Posted February 10, 2009 Ps This is the PHP script I have created so far in order to retrieve the question and answer options from the database and display the answer options with radio buttons but it displays nothing. Can someone explain why? thanks #!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 ORDER BY RAND()"; $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); // get question ID and question $QuestionID = $row->QuestionID; echo '<h2>'.$row->Question .'</h2>'; echo "<form method = post action = 'user_submit.php'>"; 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, Option2, Option3, Option4."'>'".$row->Option1, Option2, Option3, Option4."'</input><br />"; } echo "<input type = hidden name = QuestionID value = '".$QuestionID."'>"; echo "<input type = submit name = submit value = 'Submit!'>"; } echo '</form>'; } if (!mysql_query($sql,$connect)) { die ('Error' . mysql_error()); } } mysql_close($connect) ?> Link to comment https://forums.phpfreaks.com/topic/144629-retrieving-mysql-records-and-display-with-radio-buttons/#findComment-758995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.