This topic is locked

Prevent Duplicates on Edit Page

9/18/2008 3:15:50 PM
PHPRunner General questions
I
illi author

Hello,
I use this snippit of code to prevent users from creating a duplicate username on the Add Page-BeforeRecordAdded:
global $conn;

$strSQLExists = "select * from students where Username='".$values["Username"]."'";

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

$data=db_fetch_array($rsExists);

if($data)

{

echo "<center><b><font color=red>Username already exists!</font></b></center>";

return false;

}
How would I do something similar for the Edit Page to prevent someone from changing a username to a duplicate?
Thanks!

Sergey Kornilov admin 9/18/2008

Use the same code in BeforeEdit event. The only difference is that you need to run this check when username changed.

global $conn;
if ($values["Username"]!=$oldvalues["Username"])

{

$strSQLExists = "select * from students where Username='".$values["Username"]."'";

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

$data=db_fetch_array($rsExists);

if($data)

{

echo "<center><b><font color=red>Username already exists!</font></b></center>";

return false;

}

}
I
illi author 9/18/2008

That is fantastic! Works as described. Thanks so much for the quick response!