Forum Moderators: coopster
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!
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.");
}
}