Hi,
I’m trying to create a basic script where has a unique code which is then checked to see if it exists on the DB, and if the email field is empty, the record is updated with their email.
I’ve manage to get that part of the script working, but am struggling to make it so that if there’s an email already assigned to the code, the user cannot access the free download. Here’s what I’ve got so far:
[php]
<?php //form not yet submitted //display add news form if (!$_POST['submit']) { ?><form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="frmFreeTrack">
<label>Email:</label>
<input size="50" maxlength="250" type="text" name="email" />
<label>Code:</label>
<input size="50" maxlength="250" type="text" name="code" />
<input type="Submit" name="submit" value="Submit" />
</form>
<?php
}
else
{
include('config.php');
//set up error list
$errorList = array();
$code = $_POST['code'];
$email = $_POST['email'];
//Open a connection to the database
$db = mysql_connect($hostname, $user, $pass) or die ('Error connecting to mysql');
mysql_select_db($name) or die ('Error connecting to mysql');
$query = "SELECT * FROM $codesDB WHERE code='$code' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$query = "UPDATE $codesDB SET email = '$email' WHERE code='$code' ";
$result = mysql_query($query) or die(mysql_error());
print 'Download free track here';
include('download.php');
}
else
{
print 'Incorrect code';
}
}
?>[/php]
How would I go about adding, if the email field has content “this code has already been used” error?
Any pointers would be greatly appreciated.
Sean