Page 1 of 1
delete duplicate records
Posted: Fri Oct 08, 2004 12:47 pm
by nmphpwebdev
How can i check for duplicate records in a form before they are submitted to the database?
I have seen this snippet, but i can't remember where i saw it.
Posted: Fri Oct 08, 2004 1:39 pm
by hawleyjr
Code: Select all
<?php
foreach($_GET as $ak =>$val){
if(search_array_forValue($val,$_GET))
unset($_GET[$ak]);
}
/* THIS FUNCTION LOOPS THROUGH AN ARRAY AND SEARCHES FOR A SPECIFIC VALUE */
function search_array_forValue($v, $a){
//$a is array to be searched
//$v is value to search
if(!is_array($a)){
//IF A IS NOT AN ARRAY CHECK $V FOR BEING AN ARRAY
if(is_array($v)){
//SET A TO BE V AND V TO BE A
$v = $vv;
$v = $a;
$a = $vv;
}
}
if(isset($v) and isset($a) and count($a)>0){
if(array_search($v,$a) or $a[0]==$v)
return TRUE;
else
return FALSE;
}
return FALSE;
}
?>
Posted: Fri Oct 08, 2004 1:40 pm
by hawleyjr
oh yeah,
all you really need is array_search() but I have the other stuff there for error checking.
Posted: Fri Oct 08, 2004 3:05 pm
by feyd
hawleyjr, your "variable swapping" chunk assigns null to $a... and nmphpwebdev is asking about databases not arrays... so uhm, not sure what you're doing..
nmphpwebdev, you can do a selection query based on the data submitted which are supposed to be unique, like username.
If the fields that are supposed to be unique were in fact marked as unique keys or primary keys, then trying to insert a duplicate key will result in an error being fired back.
Posted: Fri Oct 08, 2004 3:12 pm
by nmphpwebdev
just remind me to "KISS" my code LMAO.
I didn't even think about that.