urgent plz help

Discussion in 'PHP' started by newbie12345, May 4, 2010.

  1. #1
    hey peeps
    i am have a little issues
    my problem is that im trying to check if more than one user name exists in the database
    and if not do an insert statement and wen its finish
    echo a message stating

    thank u '.$user.' for registering

    can anyone tell me my errors


    $duplicate = mysql_num_rows(mysql_query("select * from user_accounts where username = '$user'"));
    
    if ($duplicate > 0){
     echo  "The username '.$user.' is already taken try another.";
       }
    
    $result= mysql_query ("INSERT INTO user_accounts (username, firstname, lastname, 
    birthday, gender, address, password, 
    confirm_password, email_address)
    VALUES ('$user', '$first', '$last', '$birth', '$sex', '$addres', 
    '$pass', '$conpass', '$eaddress')");
     
    echo "thank you '$user' for registering.";
    
    
    ?>
    PHP:
     
    newbie12345, May 4, 2010 IP
  2. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #2
    After you echo that the username has already been taken, it will still process the rest of your script. Either place the ELSE {} after and include the register process between { and } or place the exit function after the echo message that the username has been taken.

    
    $duplicate = mysql_num_rows(mysql_query("select * from user_accounts where username = '$user'"));
    if ($duplicate > 0)
    { echo  "The username '.$user.' is already taken try another.";   } ELSE {
    $result= mysql_query ("INSERT INTO user_accounts (username, firstname, lastname,
     birthday, gender, address, password, confirm_password, email_address)
    VALUES ('$user', '$first', '$last', '$birth', '$sex', '$addres', '$pass', '$conpass', '$eaddress')"); 
    echo "thank you '$user' for registering.";}
    ?>
    
    PHP:
     
    Grit., May 5, 2010 IP
  3. newbie12345

    newbie12345 Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank alot man it worked
     
    newbie12345, May 5, 2010 IP
  4. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Yeah, but clean-up your code. And remember LIMIT 1 on your MySQL statement, since you want it to stop searching if a row is found:

    
    <?
    
    $duplicate = mysql_num_rows(mysql_query("SELECT * FROM user_accounts WHERE username = '".$user."' LIMIT 1"));
    
    if ($duplicate > 0){ 
    	echo  'The username '.$user.' is already taken try another.';
    } else {
    	$result = mysql_query ("INSERT INTO user_accounts (
    														username,
    														firstname,
    														lastname,
     														birthday,
     														gender,
     														address,
     														password,
     														confirm_password,
     														email_address
     													  ) VALUES (
     													   '$user',
     													   '$first',
     													   '$last',
     													   '$birth',
     													   '$sex',
     													   '$addres',
     													   '$pass',
     													   '$conpass',
     													   '$eaddress')");
    	echo 'thank you '.$user.' for registering.';
    }
    
    ?>
    
    PHP:
    You will benefit from making it right and easy to overview the first time ;)

    Furthermore, remember to escape and trim the $user variable to prevent injections.
     
    elias_sorensen, May 5, 2010 IP