Jump to content

Simple matching script with MySQL and HTML forms help


DillyDong

Recommended Posts

Hello, all, I've got a question I hope you can help me out with.

I've got a database with two columns, one is called `the_question`, and the other is `the_answer`. Say the database looks like this:

[code]
+++++++++++++++++++++++++++++
|`the_question`|`the_answer`|
|++++++++++++++++++++++++++++
|Apple        | Red        |
|Banana        | Green      |
|Grape        | Purple    |
|Blueberry    | Blue      |
|Cherry        | Red        |
+++++++++++++++++++++++++++++
[/code]

Now, what I want to do is have a form on a page where on the left-hand column, each value from `the_question` is listed, and on the right of each `the_question` value, there should be an input box where the user should enter the appropriate answer.
Now, I want to pass this data to a new page via form action, and I want the next page to check the user's responses against the appropriate `the_answer`fields. I'm having trouble understanding exactly how to do this. The script I have for the first page is:

[b]test_1.php[/b]
[code]
<form method="post" action="test_2.php">
<table>
<tr><td>Apple</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Banana</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Grape</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Blueberry</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
<tr><td>Cherry</td><td><input type="text" name="(don't know what to put here)" /></td></tr>
</table>
<input type="submit" value="Submit" />
</form>
[/code]

[b]test_2.php[/b]
[code]
<?php
foreach($_POST as $value) {
$data = $_POST; }

foreach($data as $data_for_query) {
$sql = "SELECT * FROM `table` WHERE `the_answer` = ".$data_for_query.";";
$query = mysql_query($sql);
if(empty(mysql_num_rows($query))) {
echo "Sorry, the correct answer was: " ;
//And at this point, how do I get it to return the correct answer? All I passed through the form was user input :(
else {
echo "Good job.";
}
}
?>
[/code]

Any help would be greatly appreciated! I have no idea.
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.