dink87522 Posted November 9, 2008 Share Posted November 9, 2008 I wish to build/make a a simple quiz for our office lunchtime quiz. I know enough PHP to be able to customize scripts a bit, but I do not feel confident in writing them on my won. I have no experience with mysql. This is how I hope it will work: A question is selected from a database (either randomly from all questions or from a certain category of questions i.e. Music category). That question is then displayed and the user gives their answer through a form. The answer is then checked against the stored answer in a a mysql databse to see if it is correct. Bear in mind that I'm not very qualified, how do I go about building something like this or can anyone help get me started? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 9, 2008 Share Posted November 9, 2008 No one here will write it for you, go to tizag.com w3schools.com and learn about mysql and maybe some php, and then when you make a start come back when you encounter an error. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 9, 2008 Author Share Posted November 9, 2008 I know basic PHP. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 9, 2008 Share Posted November 9, 2008 You will need to know more than <?php echo "hello world"; ?> We will not write it , go and learn what you need then return and we will help. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 9, 2008 Author Share Posted November 9, 2008 I'm not asking you too I'm just asking for some ideas on how to get started. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 9, 2008 Share Posted November 9, 2008 I have given them, go and learn how to use mysql databases and how to write php to interact with mysql and get data from forms, then return when you have started. http://www.tizag.com/mysqlTutorial/ http://w3schools.com/php/php_mysql_intro.asp Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 The working concept for the quiz is quite good. If you know PHP, then you should now learn basics of MySQL, some theory behind relational databases (so as you know the difference between rows and columns etc ), and of course follow some tutorial on connecting PHP to MySQL (this one's easy ) Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 9, 2008 Author Share Posted November 9, 2008 Thanks for the positive help. I'll post back with questions shortly. Quote Link to comment Share on other sites More sharing options...
nitation Posted November 9, 2008 Share Posted November 9, 2008 Blade and Mchl reply are correct. You need to learn all this before embarking on the project. Or alternatively, hire a developer. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 10, 2008 Author Share Posted November 10, 2008 Ok to add questions from the form (Question Tool Panel)I have <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Quiz 1</title> </head> <body> <table width="916" border="0" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr> <td width="238" height="45"> </td> <td width="335"> </td> <td width="343"> </td> </tr> <tr> <td height="308"> </td> <td valign="top"><form id="form2" name="form2" method="post" action="process.php"> <label> <div align="center"><u><strong>Question Tool Panel</strong></u></div> <br /> <strong>Category</strong> <select name="$category"> <option>Africa</option> <option>Asia</option> <option>Europe</option> <option>North America</option> <option>Oceania</option> <option>South America</option> </select> <label>User <select name="$user"> <option>Eddie</option> </select> </label> <br /> <br /> <label><strong>Question<br /> </strong> <textarea name="$question" cols="50"></textarea> </label> </form> <form id="form1" name="form1" method="post" action="process.php"> <label><strong>Answer</strong><br /> <textarea name="$answer" cols="50"></textarea> </label> <label> <br /> <br /> <input type="submit" name="Submit" value="Add question" /> (250 character limit for each text field) <br /> <br /> <div align="center"> Copyright 2008 Geography Trainer </div> </label> </form></td> <td> </td> </tr> <tr> <td height="47"> </td> <td> </td> <td> </td> </tr> </table> </body> </html> and for process.php I have: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Question Tool Panel</title> </head> <body> <?php // Make a MySQL Connection mysql_connect("localhost", "dink87522_dink", "dinky") or die(mysql_error()); mysql_select_db("dink87522_db1") or die(mysql_error()); // Insert a row of information into the table "example" mysql_query("INSERT INTO moon (question, answer) VALUES('$question', '$answer' ) ") or die(mysql_error()); echo "Data Inserted!"; ?> <a href="quiz.php">Back to Question Tool Panel </a> </body> </html> Ignore user and category for now. This is located at http://www.dink87522.oxyhost.com/quiz.php however it gives the error " Notice: Undefined variable: question in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 16 Notice: Undefined variable: answer in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 16 Data Inserted!Back to Question Tool Panel " and I'm not quite sure why. (edited by kenrbnsn to change the tags to tags) Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 10, 2008 Share Posted November 10, 2008 First the value of the name attribute in the <input> tags should not have a "$" in them. Second, it looks like you've written your PHP with the assumption that register_globals is enabled, which is a false assumption. Try this in the process.php (after you get rid of the "$" in the name attributes) <?php $q = "INSERT INTO moon (question, answer) VALUES('" . mysql_real_escape_string(stripslashes($_POST['question'])) . "', '" . mysql_real_escape_string(stripslashes($_POST['answer'])) . "'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); ?> Ken Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 10, 2008 Author Share Posted November 10, 2008 I'm sort of confused to be honest with that. So I have quiz.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Quiz 1</title> </head> <body> <table width="916" border="0" cellpadding="0" cellspacing="0"> <!--DWLayoutTable--> <tr> <td width="238" height="45"> </td> <td width="335"> </td> <td width="343"> </td> </tr> <tr> <td height="308"> </td> <td valign="top"><form id="form2" name="form2" method="post" action="process.php"> <label> <div align="center"><u><strong>Question Tool Panel</strong></u></div> <br /> <strong>Category</strong> <select name="category"> <option>Africa</option> <option>Asia</option> <option>Europe</option> <option>North America</option> <option>Oceania</option> <option>South America</option> </select> <label>User <select name="user"> <option>Eddie</option> </select> </label> <br /> <br /> <label><strong>Question<br /> </strong> <textarea name="question" cols="50"></textarea> </label> </form> <form id="form1" name="form1" method="post" action="process.php"> <label><strong>Answer</strong><br /> <textarea name="answer" cols="50"></textarea> </label> <label> <br /> <br /> <input type="submit" name="Submit" value="Add question" /> (250 character limit for each text field) <br /> <br /> <div align="center"> Copyright 2008 Geography Trainer </div> </label> </form></td> <td> </td> </tr> <tr> <td height="47"> </td> <td> </td> <td> </td> </tr> </table> </body> </html> and for process.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Question Tool Panel</title> </head> <body> <?php // Make a MySQL Connection mysql_connect("localhost", "dink87522_dink", "dinky") or die(mysql_error()); mysql_select_db("dink87522_db1") or die(mysql_error()); $q = "INSERT INTO moon (question, answer) VALUES('" . mysql_real_escape_string(stripslashes($_POST['question'])) . "', '" . mysql_real_escape_string(stripslashes($_POST['answer'])) . "'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); ?> <a href="qtp.php">Back to Question Tool Panel </a> </body> </html> This gives the error "Notice: Undefined index: question in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 14 Problem with the query: INSERT INTO moon (question, answer) VALUES(', 'thirty years' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'thirty years'' at line 1" though and I've tried to fix it although am stuck. (edited by kenrbnsn to change the tags to tags) Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 10, 2008 Share Posted November 10, 2008 It's (most likely) about $_POST['question'] not having a value set. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 10, 2008 Author Share Posted November 10, 2008 I'm sorry I don't know what you're saying. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 10, 2008 Share Posted November 10, 2008 The notice and error seem to be caused because there is nothing in $_POST['question'] variable. Which in turn is probably caused by the fact, you have two forms in your quiz.php, and only the second one is submitted (i.e. only answer is being send to process.php). Put both question and answer textareas in one form. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted November 10, 2008 Share Posted November 10, 2008 You have two forms defined in your source. The first one has the inputs named "category", "user", and "question" and does not have a submit button. The other has the inputs "answer" and has a submit button. When you press the submit button, the process.php script will only receive the values from the second form. Ken Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 10, 2008 Author Share Posted November 10, 2008 I've only got one form now. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Question Tool Panel</title> </head> <body> <?php // Make a MySQL Connection mysql_connect("localhost", "dink87522_dink", "dinky") or die(mysql_error()); mysql_select_db("dink87522_db1") or die(mysql_error()); $q = "INSERT INTO moon (question, answer) VALUES('" . mysql_real_escape_string(stripslashes($_POST['question'])) . "', '" . mysql_real_escape_string(stripslashes($_POST['answer'])) . "'"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); ?> <a href="qtp.php">Back to Question Tool Panel </a> </body> </html> gives the error Problem with the query: INSERT INTO moon (question, answer) VALUES('what is my name', 'david' You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment Share on other sites More sharing options...
.josh Posted November 10, 2008 Share Posted November 10, 2008 you forgot the closing ) in your query Quote Link to comment Share on other sites More sharing options...
sasa Posted November 10, 2008 Share Posted November 10, 2008 you need ) in the end of query $q = "INSERT INTO moon (question, answer) VALUES('" . mysql_real_escape_string(stripslashes($_POST['question'])) . "', '" . mysql_real_escape_string(stripslashes($_POST['answer'])) . "')"; Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 10, 2008 Author Share Posted November 10, 2008 i done that and it now gives teh error: "Parse error: syntax error, unexpected T_STRING in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 14" Quote Link to comment Share on other sites More sharing options...
.josh Posted November 10, 2008 Share Posted November 10, 2008 well there's nothing wrong with the code sasa posted so post your code. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 11, 2008 Author Share Posted November 11, 2008 I just messed around with it a bit and it worked. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 11, 2008 Author Share Posted November 11, 2008 Now I just have to improve it and try and recall/display the q. Quote Link to comment Share on other sites More sharing options...
dink87522 Posted November 11, 2008 Author Share Posted November 11, 2008 I've broken it :'( I tried to password protect the page. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Geography Trainer Question Tool Panel</title> </head> <body> <table width="950" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="238" height="1"> </td> <td width="335"> </td> <td width="343"> </td> </tr> <tr> <td height="308"> </td> <td valign="top"> <?php $username = "dink87522"; $password = "dinky"; if (isset($_POST['username']) &&($_POST['username'] == $username && $_POST['password'] == $password)){ //hidden <div align="center"><u><strong>Question Tool Panel</strong></u></div> <br /> <strong>Category</strong> <select name="category"> <option>Africa</option> <option>Asia</option> <option>Europe</option> <option>North America</option> <option>Oceania</option> <option>South America</option> </select> <label>User <select name="user"> <option>Eddie</option> </select> <form id="form1" name="form1" method="post" action="process.php"> <label><strong>Question<br /> </strong> <textarea name="question" cols="50"></textarea> </label> <label><strong>Answer</strong><br /> <textarea name="answer" cols="50"></textarea> <br /> <br /> <input type="submit" name="Submit" value="Add question" /> (250 character limit for each text field) <br /> <br /> <div align="center">Copyright 2008 Geography Trainer</div> </form></td> } else{ ?> <form method="POST" action=""><div align="center"> <table border="0"> <tr> <td>Username:</td> <td><input type="text" name="username"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password"></td> </tr> <tr> <td> </td> <td><input type="submit" name="submit" value="Login"></td> </tr> </table> </div> </form> </table> </body> </html> process.php: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Geography Trainer Question Tool Panel</title> </head> <body> <?php // Make a MySQL Connection mysql_connect("localhost", "dink87522_dink", "dinky") or die(mysql_error()); mysql_select_db("dink87522_db1") or die(mysql_error()); $q = "INSERT INTO moon (question, answer) VALUES('" . mysql_real_escape_string(stripslashes($_POST['question'])) . "', '" . mysql_real_escape_string(stripslashes($_POST['answer'])) . "')"; $rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error()); echo "Question successfully added; ?> <br><a href="quiz.php">Back to Question Tool Panel </a> </body> </html> http://www.dink87522.oxyhost.com/quiz.php it is saying "Parse error: syntax error, unexpected '<' in /www/oxyhost.com/d/i/n/dink87522/htdocs/quiz.php on line 22" yet I've gone through and can't for the life of me find any unexpected '<'. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 First error i saw if (isset($_POST['username']) &&($_POST['username'] == $username && $_POST['password'] == $password)){ should be if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] == $username && $_POST['password'] == $password)){//thats how i would do it, also there was a syntax error And please use CODE tags, not QUOTE You also don't close the <?php tag here if (isset($_POST['username']) &&($_POST['username'] == $username && $_POST['password'] == $password)){ //hidden <div align="center"><u><strong>Question Tool Panel</strong></u></div> or reopen it here </td> } else{ ?> 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.