Jump to content

PHP/mysql quiz help


dink87522

Recommended Posts

  • Replies 63
  • Created
  • Last Reply

Top Posters In This Topic

Are you serious, if it is in your code and you don't know what it does don't post it when you get errors. Google it, use the php manual.

 

Once my school friend asked me to help him with a program ha was writing as a homework. I was surprised it was compiling at all as he obviously glued together parts of two other programs without really understanding what they're supposed to do, or even how to call them... That was fun.... sorta...

Link to comment
Share on other sites

Sorry about not using code tags.

 

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>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']) && isset($_POST['password']) && $_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>
  <?php
}
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>

 

I made the suggested changes and its still not parsing.

"Parse error: syntax error, unexpected ')' in /www/oxyhost.com/d/i/n/dink87522/htdocs/quiz.php on line 20"

Link to comment
Share on other sites

This is fixed, and the last time you must learn how to debug, and not post every error here.

<!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']) && isset($_POST['password']) && $_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>
     <?php
}
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>
<?php
}
?>

Link to comment
Share on other sites

Oh I get it.

 

With 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>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">Go Back to Question Tool Panel</a>
</body>
</html>
<?php
?>

 

and it gives the error

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 18

Can you please explain whats wrong with that lien of javascipt/my syntax.

 

when I remove that lien of javascript that error goes away although then it gives an error about $end on line 21 for some unknown reason.

Link to comment
Share on other sites

Try

<!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());
$question = mysql_real_escape_string($_POST['question']);
$question = stripslashes($question);
$answer = stripslashes($_POST['answer']);
$answer = mysql_real_escape_string($answer);
$q = "INSERT INTO moon (question, answer) VALUES('$question','$answer')";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
echo "Question successfully added";
?>
<br>
<a href="quiz.php">Go Back to Question Tool Panel</a>
</body>
</html>

I made it easier to read and fixed the error which was

echo "Question successfully added";

You missed that closing tag, so it mucked up the rest of the script.

Link to comment
Share on other sites

So a logical error here. After you add a question in takes you back to quiz.php and requires you to 'login' again. Is there a way, without using sessions which can work around this so that you don't have to sign in again after adding each question?

Link to comment
Share on other sites

S for recalling the question:

<!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</title>
</head>

<body>
<p>Geography Trainer</p>
<p>Question: </p>
<form id="form1" name="form1" method="post" action="">
  <label>Your answer
  <input type="text" name="textfield" />
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  </label>
</form>
<p> </p>
</body>
</html>

 

and I am lost. How do I pull a question (randomly from the table column question) and then compare the user's entered answer to the question answer stored in the table?

Link to comment
Share on other sites

Without knowing your database structure it is hard to say, but something like;

<?php
//connected already
$sql = "SELECT * FROM table_questions ORDER BY RAND() LIMIT 0,1";//this way you will display one question from random from the database, you will need a way to check what questions they have answered.
$sql = mysql_query($sql) or die ("Error!<br>".mysql_error());
$sql = mysql_fetch_assoc($sql);
echo $sql['question'];//echo the question
//user form, and user input
$ans = $_POST['answer'];
if($ans == $sql['answer'])
{
echo "correct answer";
}
?>

Link to comment
Share on other sites

Note that in this case answer given will have to be worded exactly as the answer saved in database. So if the question was "President of which country is Barack Obama?", and the stored answer was "United States of America", but the user entered "USA", he would give an incorrect reply.

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.