Jump to content

Check for duplicate entries - mysql insert


thaidomizil

Recommended Posts

Hello,

 

i am inserting some form data into my mysql db, i happen to get some duplicates so i want to check first if the entry exists already before i insert.

 

my current code:

 

<?php

if(isset($_POST['submit']))  { ?>
<?php 
if (strlen($_POST['code']) == 19 && substr($_POST['code'],0,7) == '5541258')  { 
     mysql_query("INSERT INTO table(code,secret,ip,date) VALUES('$_POST[code]','$_POST[secret]','$_SERVER[REMOTE_ADDR]',CURDATE())");
Print "<font color='green'>The code will be checked now</font>"; 
} else { 
    
Print "<font color='red'>The code is invalid</font>"; 
}
?><?php } ?>

 

I would like to use the value 'code' to check if the entry exists, that one is unique for each entry. How would i do that ?

 

Thanks !

Link to comment
Share on other sites

this method will work.. however I am drawing a blank for any other methods atm..

 

if(isset($_POST['submit'])){
   if (strlen($_POST['code']) == 19 && substr($_POST['code'],0,7) == '5541258'){
      $code = $_POST['code'];
      $select_query = mysql_query("SELECT * FROM table WHERE code = '$code'");
      if(mysql_num_rows($select_query) == 0){
         $remote_addr = $_SERVER['REMOTE_ADDR'];
         $secret = $_POST['secret'];
         mysql_query("INSERT INTO table(code,secret,ip,date) VALUES('$code','$secret','$remote_addr',CURDATE())");
      }
   }
}

Link to comment
Share on other sites

You could do an "INSERT IGNORE" which would ignore duplicate rows.

 

So, you would need to make the column "code" as either a primary key or a unique key.

 

<?php
$sql = null;
if(isset($_POST['submit'])){
if(strlen($_POST['code']) == 19 && substr($_POST['code'],0,7) == '5541258')  { 
	$code = mysql_real_escape_string($_POST['code']);
	$secret = mysql_real_escape_string($_POST['secret']);
	$addr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
	$sql = mysql_query("INSERT IGNORE INTO table(code,secret,ip,date) VALUES('$code','$secret','$addr',CURDATE())");
}
}

if(mysql_affected_rows($sql) == 1){
Print "<font color='green'>The code will be checked now</font>"; 
}else{
Print "<font color='red'>The code is invalid</font>"; 
}
?>

Link to comment
Share on other sites

You could do an "INSERT IGNORE" which would ignore duplicate rows.

 

So, you would need to make the column "code" as either a primary key or a unique key.

 

<?php
$sql = null;
if(isset($_POST['submit'])){
if(strlen($_POST['code']) == 19 && substr($_POST['code'],0,7) == '5541258')  { 
	$code = mysql_real_escape_string($_POST['code']);
	$secret = mysql_real_escape_string($_POST['secret']);
	$addr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
	$sql = mysql_query("INSERT IGNORE INTO table(code,secret,ip,date) VALUES('$code','$secret','$addr',CURDATE())");
}
}

if(mysql_affected_rows($sql) == 1){
Print "<font color='green'>The code will be checked now</font>"; 
}else{
Print "<font color='red'>The code is invalid</font>"; 
}
?>

oooo, never heard of INSERT IGNORE.. i like it..seems to be the SELECT DISTINCT of the INSERT clause

Link to comment
Share on other sites

You could do an "INSERT IGNORE" which would ignore duplicate rows.

 

So, you would need to make the column "code" as either a primary key or a unique key.

 

<?php
$sql = null;
if(isset($_POST['submit'])){
if(strlen($_POST['code']) == 19 && substr($_POST['code'],0,7) == '5541258')  { 
	$code = mysql_real_escape_string($_POST['code']);
	$secret = mysql_real_escape_string($_POST['secret']);
	$addr = mysql_real_escape_string($_SERVER['REMOTE_ADDR']);
	$sql = mysql_query("INSERT IGNORE INTO table(code,secret,ip,date) VALUES('$code','$secret','$addr',CURDATE())");
}
}

if(mysql_affected_rows($sql) == 1){
Print "<font color='green'>The code will be checked now</font>"; 
}else{
Print "<font color='red'>The code is invalid</font>"; 
}
?>

 

Hello,

 

Thanks for the Code, this looks like it checks for all values is this correct ? how would i modify it to only check for the value 'code', this is the only one that has to be unique in the DB. For 'secret' for example, it's ok if there's a duplicate.

Link to comment
Share on other sites

no, it only checks the unique ones. So if the column "code" is set to unique, it will only check that column on insert. If it finds a duplicate "code" it won't insert anything, otherwise it will insert.

 

Example:

 

insert ignore into members (first_name, code) values ('jim', '123');
-- inserts 1 row

insert ignore into members (first_name, code) values ('joe', '123');
-- inserts 0 rows

insert ignore into members (first_name, code) values ('jim', '1233');
-- inserts 1 row

Link to comment
Share on other sites

I already have that in my code:

 

if(mysql_affected_rows($sql) == 1){
// The code has just been inserted (no duplicates found)
}else{
// The code already exists (its a duplicate)
}

 

I mean there needs to be one more if statement, to print the message 'duplicate entry', now there's one for code inserted and one for code invalid (if the code is not 19 digits and does not start with 5541258), so i would need another one that says duplicate entry instead of code inserted (since it is a valid code). Or not ?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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