I have code similar to the following:
$sql2 = "ALTER TABLE table ADD UNIQUE(field2, feild3, field4, field5);";
$sql2 .= "INSERT IGNORE INTO table (field1, field2, feild3, field4, field5) VALUES ('$_POST[field1]','$_POST[field2]','$_POST[field3]','$_POST[field4]','$_POST[field5]')";
if (!mysql_query($sql2,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
}
I have found that I get an error if the table already has two rows which are the same but works well otherwise. I am worried that if I add the duplicates from two different sources that they will both be added to the table and later requests will trigger the error I got earlier because of the two almost identical rows. Is it possible to add two almost identical rows as shown above if the php file is executed at the same time from two different sources?
EDIT: FYI The code is a snippet from a larger program. I am sending data from an android app I am working on to the $_POST fields
mysql_query DOES NOT ALLOW execute two queries in single call
insert ignore won’t return error when duplicate found (that’s why it named as ignore)
So, the FIRST error is caused by the
alter ...; insert ...Then alter table is require execute once only,
on your next execution, you are no longer require to perform alter table (again).
Otherwise, you will getting the duplicate INDEX error (trying to add an existing index)
For the unique index, is meant to prevent duplicate,
so, it only will have one unique record (no matter what)
And for you funny almost identical,
it will never happen
it blocked when duplication found on field 2..5