This topic is locked

prevent duplicate records

11/19/2008 8:22:22 PM
PHPRunner General questions
W
wfcentral author

my project has a customer table with id and name.
I need to prevent Someone from putting a customer in twice. the code below is not work ay because I do it know how to get the name value I just put in.
the way it should work is if Charlie Brown is in the database and they try to put him the again it will say "Charlie Brown is already in the system"
if it was really Cool it would show near matches too just in case they misspell -"Charles Brown is already in the system"
// Parameters:

// $values - Array object.

// Each field on the Add form is represented as a 'Field name'-'Field value' pair
//** Check if specific record exists ****

global $conn;

$strSQLExists = "select * from tbl_customers where name='name-$value'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists do something

}

else

{

// if dont exist do something else

}
return true;
// return true if you like to proceed with adding new record

// return false otherwise

S
swanside 11/20/2008

You can do it in your sql database. Set the field to Unique

J
Jane 11/20/2008

Hi,
see my changes in Bold:

global $conn;

$strSQLExists = "select * from tbl_customers where name='".$values["name"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

// if record exists do something

echo $values["name"]." is already in the system";

return false;


}

else

{

// if dont exist do something else

return true;

}

K
Khris 1/16/2009

How would I generate a popup window to say "This customer already exists....." with an OK button, to be used with the code above?

vin7102 1/16/2009

Hi wfcentral,
I found a neat way to display a duplicate record message in the same place where the "Record Added" message appears (in red).

global $conn;

$strSQLExists = "select * from tbl_customers where name='".$values["name"]."'";

$rsExists = db_query($strSQLExists,$conn);

$data=db_fetch_array($rsExists);

if($data)

{

// if the record exists

$message="<div class=message><<< "."Database NOT updated ......The name entered already exists.....Use another name"." >>></div>";

return false;

}

else

{
// add this next line if needed otherwise delete

$values["name"]=strtoupper($values["name"]);
return true;

}

K
Khris 1/16/2009

Perfect! This works just as well as a popup.
Thanks Vince.