Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
armlocker

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';
}
?>

Share this post


Link to post
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.

Share this post


Link to post
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

 

Share this post


Link to post
Share on other sites

Your variable is called $query, not $sql. But you don't need to print out the variable's value, as you already know exactly what the value is.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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>';

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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...
Sign in to follow this  

×
×
  • Create New...