Linkz0rs
Member
- Messages
- 247
- Reaction score
- 7
- Points
- 18
HELP: Very simple MySQL queries
Hello, I've been working on a script and I'm getting a little confused here...
I know how to grab data from a database and show the results in a list (but that's not quite what I want here)
I want the script to...
1. Ask the user to enter a code (default page)
2. Utilize POST for the same page (I don't want to have multiple pages for this, I want it all in ONE page [index.php]) which then checks the database to see if the code exists
3. If the code exists then return the result (Used, Un-Used, Doesnt exist in table)
4. If the code does exist and is used then tell the user it's already been used
5. If the code does exist and has NOT been used, then give the user a link to download a file then modify the table in the database to mark the code as Used so it thus cannot be used again
6. If the code does not exist at all, then tell the user.
I've stripped out a lot of the HTML to just very basic ECHO's.
If you can help me achieve the above ^^^^^^ I would be appreciative.
My code:
My table looks like this...
ID, Code, Validity
Hello, I've been working on a script and I'm getting a little confused here...
I know how to grab data from a database and show the results in a list (but that's not quite what I want here)
I want the script to...
1. Ask the user to enter a code (default page)
2. Utilize POST for the same page (I don't want to have multiple pages for this, I want it all in ONE page [index.php]) which then checks the database to see if the code exists
3. If the code exists then return the result (Used, Un-Used, Doesnt exist in table)
4. If the code does exist and is used then tell the user it's already been used
5. If the code does exist and has NOT been used, then give the user a link to download a file then modify the table in the database to mark the code as Used so it thus cannot be used again
6. If the code does not exist at all, then tell the user.
I've stripped out a lot of the HTML to just very basic ECHO's.
If you can help me achieve the above ^^^^^^ I would be appreciative.
My code:
Code:
<?php
// Connect to Database
$username = "my_username";
$password = "my_password";
$hostname = "localhost";
$dbh = mysql_connect($hostname, $username, $password)
or die("Cannot connect to database");
$selected = mysql_select_db("database_name",$dbh)
or die("Cannot connect to database");
// Fetch Code from POST
$couponcode = $_POST['couponcode'];
// Default - First Visit - Ask User to enter a code in the form
if (!isset($_REQUEST['couponcode'])) {
echo("<form action='index.php' method='post' id='checkcc'>
<p>Coupon Code: <input id='name' name='couponcode' class='text' /><br />
<input type='image' name='imageField' id='imageField' src='../images/submit.gif' class='send' /></p></form>");
}
// Code already used
elseif ($couponcode=="") {
echo("Unfortunately that code has already been used.");
}
// Valid Code
elseif ($couponcode=="") {
echo("Valid Code! Click here to receive your prize.");
// Enter some code to make what WAS a valid code now marked as Used so it cannot be used again
}
ou
elseif ($couponcode=="") {
echo("You did not enter a code, please go back and try again.");
}
// Code does not exist
else {
echo("Code does not exist, sorry.");
}
?>
My table looks like this...
ID, Code, Validity
Last edited: