This topic is locked
[SOLVED]

 Add record only if it doesn't exist in another table

10/28/2011 3:14:41 PM
PHPRunner General questions
T
teri.pratt@verizon.net author

I have a table named main_addresses containing a list of address (st_number, st_name, etc.). I want to add a new record to another_table only if the address being added doesn't exist in the main_table. Both tables contain the same fields. How do I do that? Before Record is added?

C
cgphp 10/28/2011

Yes, in the "Before record added" event of the another_table table:

global $conn;

$strSQLExists = "SELECT * FROM main_table WHERE field_1='".$values['field_1']."' AND field_2='".$values['field_2']."' AND field_3='".$values['field_3']."'";

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

$data=db_fetch_array($rsExists);
if($data)

{

//if record exists set the alert message and return false

$message = "This address already exists";

return false;

}
T
teri.pratt@verizon.net author 10/28/2011

Thanks, Cristian! Worked like a charm!