Click to See Complete Forum and Search --> : Preventing Duplicate Record Insertion on Page Refresh in php


madhu86
11-14-2007, 11:45 PM
Preventing Duplicate Record Insertion on Page Refresh

UI-ZEIKVK
11-15-2007, 12:21 AM
Maintain a primary key in table;

madhu86
11-15-2007, 12:34 AM
ya i already have a primary key.but when press the refresh button it is displaying " Duplicate entry".i dont want ot display this error message.

UI-ZEIKVK
11-15-2007, 12:54 AM
First check an record already exists with the key. then insert another.

NogDog
11-15-2007, 03:28 AM
Or capture the error returned by the DBMS, and if it is a duplicate key error take what ever action (or non-action) you desire. For instance, if using MySQL:

$sql = "INSERT blah blah blah";
$result = mysql_query($sql);
if(!$result)
{
if(mysql_errno() == 1062)
{
// duplicate key, so ignore, output error, or whatever
}
else
{
// some other SQL error, so log error, die(), or whatever
}
}
elseif(mysql_affected_rows()
{
// insert successful
}
else
{
// uh-oh, something weird happened
}