ke7mzp Posted June 17, 2008 Share Posted June 17, 2008 I am trying to add a practice exam section to my web site. The script needs to be able to randumly select multipal choice questions from database or some other sorce and only pick 35 questions only from a pole of about 200 questions. can anyone help me, or point me in the right direction. Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted June 17, 2008 Share Posted June 17, 2008 created a table called "questions" like this: id -- question created a table called "choices" like this: id -- question_id -- text -- correct set all the correct answers to "1" in the correct column. <?php $result = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 30"); while($row = mysql_fetch_array($result)){ $q_id = $row['question_id']; //display question here $choices = mysql_query("SELECT * FROM `choices` WHERE `question_id`='$q_id' ORDER BY RAND() "); while($row2 = mysql_fetch_array($choices){ //display answers here } } ?> and thats pretty much the jist of displaying the test Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 17, 2008 Author Share Posted June 17, 2008 how would i go about adding the questions to the database? Quote Link to comment Share on other sites More sharing options...
bluejay002 Posted June 17, 2008 Share Posted June 17, 2008 try creating admin page for that... with a field for the question, the choices and a radio button to specify the correct one... or if you plan on making multiple correct answers, use checkbox. then a save button Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 17, 2008 Author Share Posted June 17, 2008 Would anyone know how to create said page. I have almost zero knowledge of php Quote Link to comment Share on other sites More sharing options...
chronister Posted June 17, 2008 Share Posted June 17, 2008 No one here is going to create this kind of thing for you. What you are asking for is a pretty standard input form, db storage, extraction layer and display method. That may sound complicated, but what your asking is some of the most common pieces of PHP. I would suggest you start learning PHP & MySql by reading a book, there are TONS of recommendations on here if you look for them. Find the member Thorpe, he has a beginning PHP link in his sig. Or...... you can look for a pre-coded script to use. Nate Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 I have played around with this code and when I try to add a question to the database it gives me an error "Could not connect:" yet I Know that the server is working and the login information is accurate. Can someone help me Hear is the code for the form that I am trying to submit <html> <body> <form action="insert.php" method="post"> Q_ID: <input type="text" name="ID" /> <br /> Q_ID2: <input type="text" name="question_ID" /> <br /> Question: <input type="text" name="question" /> <br /> <input type="checkbox" name="Cans1" value="1" /> ANS1: <input type="text" name="ans1" /> <br /> <input type="checkbox" name="Cans2" value="1" /> ANS2: <input type="text" name="ans2" /> <br /> <input type="checkbox" name="Cans3" value="1" /> ANS3: <input type="text" name="ans3" /> <br /> <input type="checkbox" name="Cans4" value="1" /> ANS4: <input type="text" name="ans4" /> <br /> <input type="submit" value="submit" /> </form> </body> </html> Hear is the code for the insert.php page <?php $con = mysql_connect(localhost,root,********); if ($con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); $sql="INSERT INTO question_id (ID, question) VALUES ('$_POST[question_ID]','$_POST[Cans1]','$_POST[ans1]','$_POST[Cans2]','$_POST[ans2]','$_POST[Cans3]','$_POST[ans3]','$_POST[Cans4]'),'$_POST[ans4]'"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "record added"; mysql_close($con) ?> <?php $con = mysql_connect(localhost,root,qwertyztp99); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); $sql="INSERT INTO question (ID, question) VALUES ('$_POST[iD]','$_POST[question]'"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "record added"; mysql_close($con) ?> Quote Link to comment Share on other sites More sharing options...
zenag Posted June 18, 2008 Share Posted June 18, 2008 give values in single quotes $con = mysql_connect('localhost','root','qwertyztp99'); Quote Link to comment Share on other sites More sharing options...
zenag Posted June 18, 2008 Share Posted June 18, 2008 change to if not ... if (!$con) { die('Could not connect: ' . mysql_error()); } Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 still did not work. ??? Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 18, 2008 Share Posted June 18, 2008 $sql="INSERT INTO question_id (ID, question) VALUES ('$_POST[question_ID]','$_POST[Cans1]','$_POST[ans1]','$_POST[Cans2]','$_POST[ans2]','$_POST[Cans3]','$_POST[ans3]','$_POST[C You put in too many values. It should be something like, $sql="INSERT INTO question_id (ID, question, answer) VALUES ('$_POST[question_ID]','$_POST[Cans1]','$_POST[ans1]')"; By the way, you'll need to add an answer column in question_id table. Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 I still get the same error? Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 I Fixed that error but now the page I call it from keeps giving me errors Like " Parse error: syntax error, unexpected '{' in D:\server\MyApache\htdocs\New Folder\New Folder\testapp.php on line 18" Hear is the code for that <?php $con = mysql_connect(localhost,root,qwertyztp99); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); $result = mysql_query("SELECT * FROM `questions` ORDER BY RAND() LIMIT 30"); while($row = mysql_fetch_array($result)){ $q_id = $row['question_id']; //display question here $choices = mysql_query("SELECT * FROM `question_id` WHERE `question_id`='$q_id' ORDER BY RAND() "); while($row2 = mysql_fetch_array($choices){ //display answers here } } ?> Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 18, 2008 Share Posted June 18, 2008 I don't know how order by rand() would work, so try this. <?php $con = mysql_connect(localhost,root,qwertyztp99); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); $result = mysql_query("SELECT * FROM questions LIMIT 30"); while($row = mysql_fetch_array($result)){ $q_id = $row['question_id']; //display question here $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id'"); while($row2 = mysql_fetch_array($choices){ //display answers here } } ?> Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 I am still getting the same errors Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 18, 2008 Share Posted June 18, 2008 <?php $con = mysql_connect(localhost,root,qwertyztp99); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); $result = mysql_query("SELECT * FROM questions LIMIT 30"); while($row = mysql_fetch_array($result)){ $q_id = $row['question_id']; //display question here $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id'"); while($row2 = mysql_fetch_array($choices)) { //display answers here } } ?> Okay try that D: You forgot a ) after the while function. I missed it xD Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 now I am getting this error, I am now really confuesed ??? Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\server\MyApache\htdocs\New Folder\New Folder\testapp.php on line 12 Quote Link to comment Share on other sites More sharing options...
Stephen Posted June 18, 2008 Share Posted June 18, 2008 Make sure you do have a table named "questions" and "question_id" in your phpmyadmin or anything similar. Quote Link to comment Share on other sites More sharing options...
nafetski Posted June 18, 2008 Share Posted June 18, 2008 Start small. Like, real small. Use PHPmyadmin, make a junk table...put some records in. Then try to display them. Crawl, walk, run. Google "php mysql tutorial" and hit some of the beginner ones. A few hours of learning and you'll be set! Quote Link to comment Share on other sites More sharing options...
ke7mzp Posted June 18, 2008 Author Share Posted June 18, 2008 I had it working till i added this code: FROM question_id WHERE question_id='$q_id" to this line $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" ); Hear is the code for the page that is trying to display the database <?php $con = mysql_connect("localhost","root","qwertyztp99"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); { $result = mysql_query("SELECT * FROM question ORDER BY RAND() LIMIT 1"); while($row = mysql_fetch_array($result)) { $q_id = $row['question_id']; echo $row['ID'] . " " . $row['question']; echo "<br />"; } } mysql_close($con); ?> <?php $con = mysql_connect(localhost,root,qwertyztp99); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("demo", $con); $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" ); while($row2 = mysql_fetch_array($choices)) { echo "<br />"; echo $row2['ans_a'] ; echo "<br />"; echo $row['ans_b']; echo "<br />"; echo $row['ans_c']; echo "<br />"; echo $row['ans_d']; //display answers here } ?> Hear is what this displays 2 What letters must be used for the first letter in US amateur call signs? Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\server\MyApache\htdocs\New Folder\New Folder\sqltest\table2.php on line 31 Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted June 18, 2008 Share Posted June 18, 2008 if you are just learning php now, you need to do some simplier things first..... this: $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" ); must be this: $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id' " );\ you forgot to end the single quotes Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted June 18, 2008 Share Posted June 18, 2008 ok new rule for you... ALWAYS add or die(mysql_error()); to every single query or db select mysql_query("you query") or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
chronister Posted June 18, 2008 Share Posted June 18, 2008 $choices = mysql_query("SELECT * FROM question_id WHERE question_id='$q_id" ); So you have a table named question_id with a column named question_id? typically a table and column don't have the same name. This is the proper structure for this type of query. "SELECT * FROM tablename WHERE column = 'argument'" In your query, I just noticed that you forgot a ' after $q_id ok new rule for you... ALWAYS add or die(mysql_error()); to every single query or db select mysql_query("you query") or die(mysql_error()); I think that should be a rule for almost every coder. Especially newbies. I still use this line faithfully as it can help you track down an error very quickly. 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.