Jump to content
Larry Ullman's Book Forums

How To Create A Multiple Choice Radio Button Php Script


Recommended Posts

Hi there, just trying to get some practice in by creating a simple php game - can someone tell me where am I going wrong please: Thanks in advance

<?php
$con = mysql_connect('localhost', 'root', '');
mysql_select_db("krish");
?>

QUESTION 1<br><br>

How many bytes are used to represent the primitive...? <br><br>
    
    
Choose the right answer:<br><br>

<?php

$query = mysql_query("SELECT * FROM questions WHERE question_id = 'Q1'");

while ($row = mysql_fetch_assoc($query)) {
    echo $row['option1'] . '<input type="radio" name="option_1" value="option1"><br>';   
    echo $row['option2'] . '<input type="radio" name="option_1" value="option2"><br>';  
    echo $row['option3'] . '<input type="radio" name="option_1" value="option3"><br>';  
    echo $row['option4'] . '<input type="radio" name="option_1" value="option4"><br>';      
}

$var = "select from answer questions where answer ='4'";

if (isset($var)) {
    echo 'correct answer';
} else {
    echo 'wrong answer';
}
?>

Link to comment
Share on other sites

What you're doing wrong depends upon two things: your database structure and the actual results that aren't working. I would start by confirming the results of this query using a separate interface: 

SELECT * FROM questions WHERE question_id = 'Q1'

If that doesn't make it obvious what's wrong, then please share what it's working or how it's not working.

Link to comment
Share on other sites

Hello again Yoda of PHP

 

I have echoed the $sql; under the select query above and now I get the :

 

Notice: Undefined variable: sql in C:\xampp\htdocs\game\index.php on line 16

 

my database (questions) structure is simple. here are the coloumns

 

questions_record_number

question_id

question

answer

option1

option2

option3

option4

subject

 

NOTHING IS WORKING AT THE MOMENT - AND HERES WHATS SHOWING

 

===============================================================

QUESTION 1

How many bytes are used to represent the primitive...? 

Choose the right answer:

Notice: Undefined variable: sql in C:\xampp\htdocs\game\index.php on line 16
2
4
8
1
correct answer

==============================================================

 

I know its something really simple - I just cant figure it out, and is an excellent exercise for getting results from the database, useful in many situations.

 

.... over to you Sir

 

Link to comment
Share on other sites

  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...