Jump to content

storing radio buttons in mysql and then output using php


orbitter

Recommended Posts

What do you mean....

do u mean something lyk...

 

Table 1

column 1: id

column 2: question

column 3: radiobuttons

 

or...

 

column 1: id

column 2: question

column 3: correct answer

column 4: question1

column 5: question2

column 6: question3

 

thats just mysql database table structure.

im not exactly sure what you mean so plz confirm.

hi there,

 

i mean something like u said:

 

Table 1

column 1: id

column 2: question

column 3: radiobuttons

 

and then,

 

table 2

column 1: id

column 2: answers

 

but i am unsure as to how the structure of the table should be like and how to output the question with the radio buttons using php.

thank you for your help

Okay now i understand what your are trying to do....

 

Okay first you need to create tables which are lyk this....

 

Table 1

column 1: id
column 2: question
column 3: answer1
column 4: answer2
column 5: answer3
column 6: answer4

 

and then,

 

table 2

column 1: id
column 2: answers
column 3: questionid

 

do u no php? if not ill create a quick snippet for u.

ok thanks :-) . just wanted to make sure if this is how i would create my tables? i am unsure about the question id in the second table and how i meant to put it in to the table.

 

CREATE TABLE one (

  id int(1) NOT NULL auto_increment,

  question varchar(65) NOT NULL default '',

  answerone varchar(65) NOT NULL default '',

  answertwo varchar(65) NOT NULL default '',

  answerthree varchar(65) NOT NULL default '',

  answerfour varchar(65) NOT NULL default '',

 

  PRIMARY KEY  (id)

) TYPE=MyISAM;

 

 

 

CREATE TABLE two (

  id int(1) NOT NULL auto_increment,

  answers varchar(65) NOT NULL default '',

   

 

  PRIMARY KEY  (id)

) TYPE=MyISAM;

 

Personally, I would use the following table structure:

 

table 1

----------------------

column 1: id (primary key)

column 2: question

 

table 2

---------------------

column 1: question_id (foreign key)

column 2: answer

column 3: correct( enum: '0', '1' )

 

This way, you can add more answers to a question. The question_id links to a question(id) in the first table.

 

 

koay and heres a script for the questionairee is thats what your making...

 

<?
//YOUR DB CONNECTION HERE

$getquestion= mysql_query("SELECT * FROM table1");
$num= mysql_num_rows($getquestion);

if ($num == "0"){
echo "There are currently no questions";
}else{
$the= mysql_fetch_object($getquestions); //GET FIRST OR ONLY QUESTION FROM DB
?>

<form method="post" action="">
Question: <?= ucfirst($the->question); ?><br /><br />
<input type="radio" name="answer" value="<?= $the->answerone ?>"> <?= ucfirst($the->answerone); ?><br />
<input type="radio" name="answer" value="<?= $the->answertwo ?>"> <?= ucfirst($the->answertwo); ?><br />
<input type="radio" name="answer" value="<?= $the->answerthree ?>"> <?= ucfirst($the->answerthree); ?><br />
<input type="radio" name="answer" value="<?= $the->answerfour ?>"> <?= ucfirst($the->answerfour); ?><br />
<input type="hidden" name="id" value="<?= $the->id ?>">
<input type="submit" name="submit" value="Check"><br /><br />
</form>
<?
if (strip_tags($_POST['answer']) && strip_tags($_POST['submit'])){
$answer= $_POST['answer'];
$i= $_POST['id'];

//NO NEED FOR SECURITY BECAUSE THE DATA FROM THE INPUT STUFF ARE NOT CHANGEABLE

$select= mysql_fetch_object(mysql_query("SELECT * FROM table1 WHERE id='$i'"));

if ($answer != $select->correctanswer){
echo "Your answer ($answer) was incorrect!";
}else{
echo "Your answer '$answer' was correct!";
}}
?>


<? } ?>

 

make sure you have added the correctanswer column to table1

nope its meant to show the question and then let the user answer it.

if the answer is wrong then pow and if the answer is right then wow =) basically a message comes up saying whether or not ur answer was wrong or right.

 

do u hav the script hosted anywhere?

nothing seems to be happening, just an empty page?

 

the table i created was:

 

CREATE TABLE one (

  id int(1) NOT NULL auto_increment,

  question varchar(65) NOT NULL default '',

  answerone varchar(65) NOT NULL default '',

  answertwo varchar(65) NOT NULL default '',

  answerthree varchar(65) NOT NULL default '',

  answerfour varchar(65) NOT NULL default '',

  correctanswer varchar(65) NOT NULL default '', 

  PRIMARY KEY  (id)

) TYPE=MyISAM;

 

 

 

and the script which you provided me and i edited to suit the table was:

 

<?

//YOUR DB CONNECTION HERE

 

mysql_connect("localhost", "root", "123456") or die(mysql_error());

mysql_select_db("orbit") or die(mysql_error());

 

$getquestion= mysql_query("SELECT * FROM one");

$num= mysql_num_rows($getquestion);

 

if ($num == "0"){

echo "There are currently no questions";

}else{

$the= mysql_fetch_object($getquestions); //GET FIRST OR ONLY QUESTION FROM DB

?>

 

<form method="post" action="">

Question: <?= ucfirst($the->question); ?><br /><br />

<input type="radio" name="answer" value="<?= $the->answerone ?>"> <?= ucfirst($the->answerone); ?><br />

<input type="radio" name="answer" value="<?= $the->answertwo ?>"> <?= ucfirst($the->answertwo); ?><br />

<input type="radio" name="answer" value="<?= $the->answerthree ?>"> <?= ucfirst($the->answerthree); ?><br />

<input type="radio" name="answer" value="<?= $the->answerfour ?>"> <?= ucfirst($the->answerfour); ?><br />

<input type="hidden" name="id" value="<?= $the->id ?>">

<input type="submit" name="submit" value="Check"><br /><br />

</form>

<?

if (strip_tags($_POST['answer']) && strip_tags($_POST['submit'])){

$answer= $_POST['answer'];

$i= $_POST['id'];

 

//NO NEED FOR SECURITY BECAUSE THE DATA FROM THE INPUT STUFF ARE NOT CHANGEABLE

 

$select= mysql_fetch_object(mysql_query("SELECT * FROM one WHERE id='$i'"));

 

if ($answer != $select->correctanswer){

echo "Your answer ($answer) was incorrect!";

}else{

echo "Your answer '$answer' was correct!";

}}

?>

 

 

<? } ?>

 

 

 

 

also could you please show me how to store the answers of the question into another table?

thank you for your time

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.