This topic is locked

Duplicate checking on "Edit" page

12/9/2009 12:20:02 PM
PHPRunner General questions
S
scotthallett author

Greetings all!
I have an "employees" table, I have an add and edit page for this table.
I have a "check for duplicates" for the "email" field working on the add page - and it is working great! <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=question&id=13306&image=1&table=forumtopics' class='bbc_emoticon' alt=':)' />
Problem is that I want to make sure that on the edit page, the email can't be changed to another email that is already in the DB.
So, I apply the same "check for duplicates" code from the add page - but the problem is, is that even if the email address isn't updated, it still complains that the address is already in the database.
This of course is due to the fact that the check is simple - in that it checks to see if that email that is being re-submitted (the entire set of fields gets re-submitted on the edit page) is present - even if it is its own row.
So, I know how I would do this in straight PHP:
1.) I would do a pre-check to see if the previous data (original row data before form submit) and new data (after form submit) were the same on the email field - if the same, do nothing. If different (meaning the email was changed) then apply the duplicate check.
-or-
2.) Modify my "check for duplicate" query to select employee_id AND email, and when doing the "duplicate" check, somehow exclude the employee id (using a WHERE and NOT clause) that we are working with.
For the second, I am not sure of SQL code to use (I was thinking of something like this?):

$strSQLExists = "select employee_id,email from employee where email='$values[email]' AND employee_id NOT='$values[employee_id]'";


I can experiment with this, but was wondering if there was a more simple/effetive way with PHPRunner, or if anyone has come across this before - or just some good coding standards recommendations!
Thanks all!

S
scotthallett author 12/9/2009

I saw the $oldvalues, and decided to work with this array - perfect for this task!
This seems to work well:

if ( "$values[email]" == "$oldvalues[email]" ) {
return true;
} elseif ( "$values[email]" != "$oldvalues[email]" ) {
//********** Check if specific record exists ************

global $conn;

$strSQLExists = "select email from employee where email='$values[email]'";

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

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

$message = "<div class=message>The email address: \"$values[email]\"

is already in the system - User not updated!!!</div>";

return false;

} else {

return true;

}
}
A
ashumak 12/13/2009

Where did you put this? I am looking to do something similar...

S
scotthallett author 12/14/2009



Where did you put this? I am looking to do something similar...


I treated it the same as the dup checking on the add page.
So, do it on the edit page - as a "before record updated" event.
Good luck - Hope this helps!