Forum Moderators: coopster

Message Too Old, No Replies

Duplicate Entry Checking

         

Pico_Train

3:25 pm on Jun 19, 2006 (gmt 0)

10+ Year Member



Hi there,

I am relatively new at PHP and am trying to write to the DB but check for duplicate entries first. This is what I have so far but it just doesn't work properly.

Here's my crap code:

$duplicate="SELECT email FROM `email_addresses` WHERE email= .'$email'.";
$mysql_result=mysql_query($duplicate);

$num_rows=mysql_num_rows($mysql_result);

if ($num_rows)

{
echo "That email address is already in use, please use a different email address to register.";
}

else

{

$sql = "Insert INTO email_addresses (first, last, email, date)
VALUES
('".$_POST["first"]."', '".$_POST["last"]."', '".$_POST["email"]."', '".$_POST["date"]."')";
$add = mysql_query($sql, $db);

if ($add)
{
echo ("You have been successfully added.");
}
}

Any help would be greatly appreciated, thanks!

eelixduppy

3:46 pm on Jun 19, 2006 (gmt 0)



I would first suggest change the code to the following to identify the specfic error:

error_reporting(E_ALL);
//db connection and whatnot
$duplicate="SELECT email FROM email_addresses WHERE email= '".mysql_real_escape_string($email)."'";
$mysql_result=mysql_query($duplicate) or die(mysql_error());
$num_rows=mysql_num_rows($mysql_result) or die(mysql_error());
if ($num_rows)
{
echo "That email address is already in use, please use a different email address to register.";
}
else
{
$sql = "Insert INTO email_addresses (first, last, email, date)
VALUES
('".mysql_real_escape_string [us2.php.net]($_POST["first"])."', '".mysql_real_escape_string($_POST["last"])."', '".mysql_real_escape_string($_POST["email"])."', '".mysql_real_escape_string($_POST["date"])."')";
$add = mysql_query($sql, $db) or die(mysql_error());
if ($add)
{
echo ("You have been successfully added.");
}
}

This should produce the proper errors if there is any, also i fixed a syntax error and added some security. Good luck ;)

Pico_Train

4:40 pm on Jun 19, 2006 (gmt 0)

10+ Year Member



Cool, thanks, will have a go tomorrow morning, home time!