1

I'm sure I have a simple syntax error in my code but I can't find it. The way this section of code should work is that the system checks if a userID exists, it is does or if the field is left empty, it creates an error. If it is ok it moves on to the next field which is userName and performs the same checks. Both of them report whether the corresponding value has been used before but neither of them report whether the field has been left empty.

if($userID != ""){
    $sql = "SELECT * FROM tbl_access WHERE userID = '$userID'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
        if($num_rows == 0){
            $error = "User ID not found, please enter your user ID";
        }
}
else if($userID == ""){
    $error = "Please enter your User ID";
}
else if($username != ""){
    $sql = "SELECT * FROM tbl_access WHERE userName = '$username'";
    $result = mysql_query($sql) or die(mysql_error());
        if(!empty($result)){
            $error = "Username already in use, please select a different username";
        }
}
else if($username == ""){
    $error = "Please enter a username";
}

The script works fine when checking the user ID part but it completely ignores the username part. I'm sure it must be a simple syntax error on my part but I've been staring at the same part of code for hours now so any help will be gratefully recieved

4
  • which error you are getting write something so that i will help and sort out your problem... Commented Nov 22, 2012 at 12:43
  • Hi, I'm not getting an error, it just ignores the username section Commented Nov 22, 2012 at 12:45
  • i will try i think your condition are not corrects..... Commented Nov 22, 2012 at 12:50
  • Thanks, if I comment out the userid part and the username section works Commented Nov 22, 2012 at 12:52

3 Answers 3

1

When writing nested if-else statements to choose between several alternatives use some consistent layout such as the following:

if ( condition1 )
statement1 ;
else if ( condition2 )
statement2 ;
. . .
else if ( condition-n )
statement-n ;
else

statement-e ;

if($userID != ""){
        statements
       if($num_rows == 0){
            $error = "User ID not found, please enter your user ID";
        }
}
else($userID == ""){
    $error = "Please enter your User ID";
}
 if($username != ""){
           statements
           if(!empty($result)){
            $error = "Username already in use, please select a different username";
        }
}
else($username == ""){
    $error = "Please enter a username";
}

try this ........

Sign up to request clarification or add additional context in comments.

4 Comments

are you getting userid from another form.it is little complex.print_r($_POST)->print any value.
yes, userID is from another table, however, there is a userID field in the table I'm testing with so that isn't it. The problem is that it will either check the userid or it will check the username but it won't check both
In the end I did the mysql queries separately outside of the if else and then it worked. Strange... Thanks for taking the time to help me though
thank you for your positive reply.happy to know your problem is solved out."{if you like my post point me up}". have a nice day.
1

You are trying to do too much inside of one if statement. Remember that if you fall into any one of the if or else if statements that entire if statement is over. You should break this up into smaller statements to achieve what you are expecting.

if($userID != "")
{
    $sql = "SELECT * FROM tbl_access WHERE userID = '$userID'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);

    if($num_rows == 0)
    {
        $error = "User ID not found, please enter your user ID";
    }
}
else
{
    $error = "Please enter your User ID";
}


if($username != "")
{
    $sql = "SELECT * FROM tbl_access WHERE userName = '$username'";
    $result = mysql_query($sql) or die(mysql_error());

    if(!empty($result))
    {
        $error = "Username already in use, please select a different username";
    }
}
else
{
   $error = "Please enter a username";
}

If you do something like the above statement then you should be ok. This will validate both variables and take actions if needed on either of them individually. You could shorten this even more by doing this:

If(is_null($userID) || is_null($username))
{
    //output your error message here
} else
{
    //you know that both are populated, run your queries now
}

2 Comments

Hi thanks for your help but if I use that it completely ignores the section regarding the userID. If if remove the section for the username the userid section then work
Using the second chunk of code you would do both the userID and the username in the else section. The if assumes the user is supposed to enter something in those fields or they should be populated. The top code may be the way you need to process this if one or the other could be empty.
0

Remove the else before the third if:

if($userID != ""){
    $sql = "SELECT * FROM tbl_access WHERE userID = '$userID'";
    $result = mysql_query($sql) or die(mysql_error());
    $num_rows = mysql_num_rows($result);
        if($num_rows == 0){
            $error = "User ID not found, please enter your user ID";
        }
}
else if($userID == ""){
    $error = "Please enter your User ID";
}
if($username != ""){
    $sql = "SELECT * FROM tbl_access WHERE userName = '$username'";
    $result = mysql_query($sql) or die(mysql_error());
        if(!empty($result)){
            $error = "Username already in use, please select a different username";
        }
}
else if($username == ""){
    $error = "Please enter a username";
}

3 Comments

Hi, thanks for that but when I do that it completely ignores the userID part and moves on to the username part
As I understand it, that means that the userID is right ;). What do you want to happen, if the id is correct?
I have tested it by leaving the userID blank or by entering one that doesn't exist and it completely ignores it. If the ID is missing or incorrect it should say so, if not it should move onto the next filed in the form and validate that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.