orbitter Posted March 1, 2008 Share Posted March 1, 2008 hi there, i was just wondering if it is possible to store a for example a question into a mysql database which consists of multiple answers using radio buttons, and then to output these questions using php. any help would be appreciated, thank you. Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 i am very basic with php at the moment. if you could help me with the php code, it would be much appreciated. thank you Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 Okay one second give me about 4 minutes plz im just making something for another thread Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 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; Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 just make the "questionid" with an int(11) and no default. sorry for not replying :S its becuz i had friends coming round ill make the tutorial l8er on Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 thanks alot . also for making me the tutorial later on, really appreciate it Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 do u want me to do it so it shows multiple questions? Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 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. Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 Hmm good point but u have to consider what hes trying to do... Orbitter delete table 2 and add a column called correctanswer to table1 Quote Link to comment Share on other sites More sharing options...
QuietWhistler Posted March 1, 2008 Share Posted March 1, 2008 Well, for each answer for that question, he can make a radio button with the help of a for loop, with the answer id as a value. I always use this kind of structure for polls etc. Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 yes but he is going to have more than 1 answer (multiple choice question). Well.... actually it depends on what orbitters making... Orbitter what are you making? a poll or a questionaire? Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 hey, there is no right or wrong answer within the questionnaire... every questions should come up in a new page once 1 question is answered. is that possible to make Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 sorry lol that means id have to remake the whole code kindof. sorry dont have that kinda time. Try learning from my code and fiugring out how to do it Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 ok thank you for the help though. appreciate it . i am a bit confused as to how i should make the table though, so i do not need table 2 anymore? and just add the new column that u told me to table 1? Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 yup Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 sorry to disturb again, but i am running the script. what is it exactly meant to do? does it store the answer of the question in the database? Quote Link to comment Share on other sites More sharing options...
ILYAS415 Posted March 1, 2008 Share Posted March 1, 2008 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? Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 no i only hve it running on my apache web server locally. the script doesnt seem to be outputting the question that i entered for some reason, also how can i store that answer into another table? sorry to be a bother Quote Link to comment Share on other sites More sharing options...
orbitter Posted March 1, 2008 Author Share Posted March 1, 2008 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.