Jump to content

PHP/mysql quiz help


dink87522

Recommended Posts

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.

 

Thanks Blade I'll have a look at that later.

 

Mchl I understand this, although as far as I am aware there is not any way around this unless both America and USA were defined as correct answers somehow. Its kind of a little bit of a problem when spelling an answer such as "What is the capital of Bukina Faso?", in which the answer is (really) "Ouagadougou".

Link to comment
Share on other sites

  • Replies 63
  • Created
  • Last Reply

Top Posters In This Topic

I called the table in my databse 'moon'.

 

so for question.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</title>
</head>

<body>
<?php
mysql_connect("localhost", "dink87522_dink", "dinky") or die(mysql_error());
mysql_select_db("dink87522_db1") or die(mysql_error());
//connected already
$sql = "SELECT * FROM moon 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);

?>
<p>Geography Trainer</p>
<p>Question: <?php
echo $sql['question'];//echo the question
?>
<form id="form1" name="form1" method="post" action="">
  <label>Your Answer
  <input type="text" name="a$ns" />
  </label>
  <label>
  <input type="submit" name="Submit" value="Submit" />
  <?php
  //user form, and user input
$ans = $_POST['answer'];
if($ans == $sql['answer'])
{
echo "correct answer";
}
?>
  </label>
</form>
<p>  
  
<p> </p>

</body>
</html>

 

first it gives the notice

Notice: Undefined index: answer in /www/oxyhost.com/d/i/n/dink87522/htdocs/question.php on line 30

for $_POST and I don't know why.

 

The above does recall the question although I think my form submit but is all screwed up as when you answer a question currently, it sometimes says you are correct and sometimes says you are not.

Link to comment
Share on other sites

I've broke it.

 

process.php

 <?php include("header.php"); ?>
</td>
  </tr>
</table>
<table width="780" border="0" cellspacing="0" bgcolor="#51A2A2" cellpadding="0" align="center">
  <tr> 
          <td height="16" bgcolor="#008ACC"> 
            <?php include("menu1.php"); ?>
					       
            </p></td>
  </tr>
</table> 
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="25" bgcolor="#FFFFFF" valign="top"><table width="92%" border="0" align="center" cellspacing="20">
      <!--DWLayoutTable-->
              <tr> 
                <td width="20%" height="586" valign="top">
							<?php include("menu2.php"); ?>
							</td>

                <td width="537" valign="top"> 
                  <h4 align="justify"><b><?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);
$question = mysql_real_escape_string($_POST['category']);
$question = stripslashes($category);
$answer = stripslashes($_POST['user']);
$answer = mysql_real_escape_string($user);
$q = "INSERT INTO moon (question, answer, category, user) VALUES('$question','$answer', $category', $user')";
$rs = mysql_query($q) or die("Problem with the query: $q<br>" . mysql_error());
echo "Question successfully added";
?><br>
<a href="qtp.php">Back to Question Tool Panel</a>
<br></b> </h4>
                  <h4> </h4>
                </td>
              </tr>
            </table>
             <?php include("footer.php"); ?>
    </td>
  </tr>
</table></td>
  </tr>
</table>
</body>
</html>

 

qtp.php

 <?php include("header.php"); ?>
</td>
  </tr>
</table>
<table width="780" border="0" cellspacing="0" bgcolor="#51A2A2" cellpadding="0" align="center">
  <tr> 
          <td height="16" bgcolor="#008ACC"> 
            <?php include("menu1.php"); ?>
		       
            </p></td>
  </tr>
</table> 
<table width="780" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="25" bgcolor="#FFFFFF" valign="top"><table width="92%" border="0" align="center" cellspacing="20">
      <!--DWLayoutTable-->
              <tr> 
                <td width="20%" height="586" valign="top"> 
                  <?php include("menu2.php"); ?>
			  </td>

                <td width="537" valign="top"> 
                                   <?php
$username = "dink87522";
$password = "dinky";
if (isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] == $username && $_POST['password'] == $password)
{
      //hidden
     ?>
                <u><strong>Question Tool Panel</strong></u><br>
                <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>
         </h4>
                 <form id="form1" name="form1" method="post" action="process.php">
           <label><br><strong>Question<br />
        </strong>
        <textarea name="question" cols="62"></textarea>
        </label>
        <label>
	<strong><br>Answer</strong><br />
        <textarea name="answer" cols="62"></textarea>
               <br />
          <br />
          <input type="submit" name="Submit" value="Add question" />
          (250 character limit for each text field) <br />
          <br />
        		<</td>
  </tr>
</table>
<table width="780" border="0" cellpadding="0" cellspacing="0" align="center">
  <tr bgcolor="#003333"> 
          <td height="5" bgcolor="#006699"> 
            <div id="navcontainer" align="center"> <a href="http://www.google.com"><font color="#FFFFFF" size="1" face="Verdana, Arial, Helvetica, sans-serif"> 
              Copyright 2008</font></a></div>
         </form>
	  <?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>
   		 
  <h4> </h4>
                                    <p align="justify"></td>
              </tr>
            </table>
             <?php include("footer.php"); ?>
    </td>
  </tr>
</table></td>
  </tr>
</table>
</body>
</html>
<?php
}
?>

 

i tried to add user/category and it broke it. I added these to the table moon.

Link to comment
Share on other sites

It seems so obvious once its pointed out. Thanls. Its owkring now but is giving lots of notices

Notice: Undefined index: category in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 31

Notice: Undefined variable: category in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 32

Notice: Undefined index: user in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 33

Notice: Undefined variable: user in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 34

Notice: Undefined variable: category in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 35

Notice: Undefined variable: user in /www/oxyhost.com/d/i/n/dink87522/htdocs/process.php on line 35

 

why is the variables (category and user) any more undefined than the variables question of answer?

Link to comment
Share on other sites

Undefined index or variable means it isn't defined.

 

<?php
$var = "test";
echo $v;//will create an error Undefined variable:

//or
echo $_POST['test'];/*will create the error Undefined index: as will $rows['test']; when there is no row in the table called test*/
?>
<form method="post">
<input name="test1">
</form>

Link to comment
Share on other sites

  • 7 months later...

Sorry for my noobiness. I didn't get much further after this thread on my PHP conquest. I've since taken it back up again recently (not this program, but PHP) after learning BASIC and the basics of that has set me up well for PHP, and I am going along far better this time I believe.

 

Thanks to all those who helped, and sorry about posting every error lol (I can debug most of these myself now lol).

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.