This topic is locked

Check if the value exist

3/12/2008 2:30:36 PM
PHPRunner General questions
F
Fawaz author

Is there a way to avoid PHP crashing, in case of trying to enter new duplicated value for a unique key?

For example, username is unique. When a user tries to enter a username which is already exist, the system crashes.

Can we add a check function to display user-friendly message?

Thanks.

Fawaz

S
spintz 3/12/2008

In your BeforeAdd event, you need to query the database for the username. So, let's say the table is USERS and the field is USERNAME.

function BeforeAdd(&$values,&$message,$inline)

{

global $conn;

$s = "SELECT COUNT(username) FROM users WHERE username LIKE '" . $values['username'] . "'";

$r = db_query( $s, $conn );

$d = db_fetch_numarray( $r );

$count = $d[0];

if( $count > 0 )

{

$message = "That username is already taken. Please try a different username";

return false;

}

return true;

}


Or is this for the built-in user system? I use my own custom user system, so this is how I do it.