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

... woops - my bad ... here's what is now showing after echo $query;

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

QUESTION 1

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

Choose the right answer:

Resource id #42
4
8
1
correct answer

Link to comment
Share on other sites

  • 2 weeks later...

Please use the MySQLi (notice the "i") family of functions, not the old and out-of-date MySQL functions.

After that, add the following code below where you set up your DB connection to make sure that it's okay:

echo '<pre>';
print_r($con);
echo '</pre>';
Link to comment
Share on other sites

 Share

×
×
  • Create New...