delete duplicate records

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

delete duplicate records

Post 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.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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;
}

?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

oh yeah,

all you really need is array_search() but I have the other stuff there for error checking.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
nmphpwebdev
Forum Newbie
Posts: 13
Joined: Fri Oct 01, 2004 12:29 pm

Post by nmphpwebdev »

just remind me to "KISS" my code LMAO.

I didn't even think about that.
Post Reply