Jump to content

PHP/mysql quiz help


dink87522

Recommended Posts

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?

Link to comment
Share on other sites

  • Replies 63
  • Created
  • Last Reply

Top Posters In This Topic

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 :P ), and of course follow some tutorial on connecting PHP to MySQL (this one's easy :) )

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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)

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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{
   ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.


×
×
  • 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.