Jump to content

[SOLVED] If a record matches an existing record do nothing


jefffan24

Recommended Posts

So I don't know if I should put this here or in mysql, but what my script is for is for people to add their emails to our mailing list.  Pretty simple, I got that working.  But what I want it do now is that if somebody puts their email in and their email is already added to our database then it doesn't do anything.  but if their email isn't in our database then it will add it.

 

So basically stop duplicate records.  Is there any way to do this php?  I'm sure there is, its just not coming to me.

Don't see how my code will help in this situation as it doesn't really do anything beside enter an input into a field in a database, but what ever here it is.

 

$sql="INSERT INTO emails (Email)
VALUES
('$_POST[email]')";



if (mysql_query($sql,$con))
{
echo "";
}
else trigger_error("SQL", E_USER_ERROR);

mysql_close($con)	

$email = mysql_real_escape_string($_POST['email']);
$sql = "SELECT Email FROM emails WHERE Email = '$email'";
$result = mysql_query($sql);

if(mysql_num_rows($result) == 0) {
   $sql = "INSERT INTO emails (Email) VALUES ('$email')";
   $result = mysql_query($sql);
}

$email = mysql_real_escape_string($_POST['email']);
$sql = "SELECT Email FROM emails WHERE Email = '$email'";
$result = mysql_query($sql);

if(mysql_num_rows($result) == 0) {
   $sql = "INSERT INTO emails (Email) VALUES ('$email')";
   $result = mysql_query($sql);
}

 

That's why posting your code ALWAYS helps.  As well as fixing the problem cags has also escaped your email $_POST, which before was being entered directly into your MySQL statement - a MAJOR security flaw!

 

Nice solution cags.

 

;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.