How to prevent a duplicate record

HI everybody. Ia am not that expert as a developer.
Anyway i need to check if an existing record already exists before it is inserted in to the db.
i have created a php method with this:

sc_lookup(check_record, “SELECT COUNT(idpatient) from patients WHERE name = '”.{name}."’ AND lastname = ‘".{lastname}."’ AND date_birth = ‘".{date_birth}."’");

$check_record = {check_record[0][0]};

if ($check_record > 0) {
$error_message = ‘RECORD ALREADY PRESENT’; // Error message

sc_error_message($error_message);

}

then i call it in the OnBeforeInsert event of a form.

If i try to insert a new record with those three fields equals to those of an already present patient, the error message appears but the record is inserted empty after that. I do not want that. I want it to shows the error message and stop.
I am pretty sure i am not doing it in the proper way. Could you please give me and advice?
Thank you

Hi, I think you have to use sc_error_exit(); after sc_error_message(); in order to stop the insert from happening.

Yes Just realized. Thank you

You could also put an index on your database table that prevents a duplicate on First, Last, Date-of-birth

1 Like