Before inserting into the database, I\'m using the following code to check for duplicates. To me, a duplicate is only considered a duplicate when name
, de
In short you need to do your checking through PHP and you also need to add a composite unique constraint in your MySQL table to get the best of both worlds.
it would be good if we broke down your question into two separate questions.
1- How do I check duplicates using PHP and notify about the user before executing query? 2- How do I specify a composite unique constraint in MySQL?
First, you need a simple PHP function to check whether this record exist in the DB or not, like the one below:
function is_exist($table, $data){
$sql = "SELECT * FROM `" . $table . "` WHERE ";
foreach ($data as $key => $val) :
$sql .= "`$key`='" . $val . "' AND ";
endforeach;
$sql = substr($sql,0, -5);
$result = $mysql_query($sql);
$count = $mysql_num_rows($result);
return ($count > 0) ? true: false;
}
You should call your PHP function like this way below:
$data = array('column1'=>$_POST['value'],'column2'=>$_POST['value'], ...);
if(is_exist($data)){
// Print your error message
}else{
// Run your insert query
}
This way you can prevent duplicates before going to the MySql Database, but in order to have a duplicate free database, you need to add a composite unique constraint in your MySQL table.
This simple SQL command can do the trick:
alter table `tablename` add unique index(name, description, manufacturer, city, price, enddate);
I hope that helps, if I mess anything please burden me.