Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    Checking a database if username or email already used

    I have my code:

    PHP 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?

  • #2
    Regular Coder
    Join Date
    May 2006
    Posts
    101
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try this query, and put before the INSERT query

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

  • #3
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post
    what is the function to check how many rows returned? I will need to say if rows returned !=0 then exit the script. Correct?

  • #4
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,088
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Code:
    $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
    }
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #5
    Regular Coder
    Join Date
    May 2006
    Posts
    101
    Thanks
    0
    Thanked 1 Time in 1 Post
    Code:
    $sql = " SELECT id FROM admin_login
    WHERE username='$username' OR email = '$email' ";
    $r = mysql_query($sql);
    if (mysql_num_rows($r)>0) exit;


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •