...

View Full Version : Checking a database if username or email already used



thesavior
06-10-2006, 05:10 PM
I have my code:


mysql_connect($db_host, $db_username, "newpwd");
mysql_select_db("powersoft");
$username = $_POST['username'];
$username = mysql_real_escape_string($username);
if ($username == '') exit ('You must enter a username');
$password = $_POST['password'];
if ($password == '') exit ('You must enter a password');
if ($password == 'Password') exit ('You must enter a password');
$encpassword = md5($password);
$email = $_POST['email'];
if (email == '') exit ('You must enter an email address');
if (email == 'E-mail') exit ('You must enter an email adress');
$email = mysql_real_escape_string($email);
$ip = $_SERVER['REMOTE_ADDR'];
$sql = 'INSERT INTO `admin_login` (`id`, `username`, `password`, `email`, `ip`, `user`) VALUES (NULL, \''.$username.'\', \''.$encpassword.'\', \''.$email.'\', \''.$ip.'\', \'1\');';
mysql_query($sql) or die (mysql_error());

and i want to run a check before I insert into the database whether there are any other rows with either that username, that email, or both and return an error and stop if there is. What query would I run for that and where would I put it?

harsh789
06-10-2006, 06:20 PM
Try this query, and put before the INSERT query


$sql = " SELECT id FROM admin_login
WHERE username='$username' OR email = '$email' ";

thesavior
06-10-2006, 07:55 PM
what is the function to check how many rows returned? I will need to say if rows returned !=0 then exit the script. Correct?

vinyl-junkie
06-10-2006, 11:34 PM
$rows_returned = " SELECT count(id) FROM admin_login
WHERE username='$username' OR email = '$email' ";
if ($rows_returned == 0) {
// row insert logic
} else {
// exit your script
}

harsh789
06-12-2006, 04:08 PM
$sql = " SELECT id FROM admin_login
WHERE username='$username' OR email = '$email' ";
$r = mysql_query($sql);
if (mysql_num_rows($r)>0) exit;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum