[RESOLVED] if-else problem
Results 1 to 7 of 7

Thread: [RESOLVED] if-else problem

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    11

    resolved [RESOLVED] if-else problem

    HTML Code:
    if ($db_found) {
    
    		$uname = quote_smart($uname, $db_handle);
    		$pword = quote_smart($pword, $db_handle);
    
    		$SQL = "SELECT * FROM login WHERE username = $uname AND password = $pword";
    		$result = mysql_query($SQL);
    		$num_rows = mysql_num_rows($result);
    
    
    		if ($result) {
    			if ($num_rows > 0) {
    				if ($uname =='abc')
    				{
    				session_start();
    				$_SESSION['login'] = "1";
    				header ("Location: welcomeabc.php");
    				}
    				else if($uname =='xyz')
    				{
    				session_start();
    				$_SESSION['login'] = "1";
    				header ("Location: welcomexyz.php");
    					
    				}
    				else
    				{session_start();
    				$_SESSION['login'] = "1";
    				header ("Location: login.php");
    					}
    			}
    			else {
    				//$errorMessage = "Invalid Login";
    				//session_start();
    				//$_SESSION['login'] = '';
    
    				session_start();
    				$_SESSION['login'] = "";
    				header ("Location: login.php");
    			}	
    		}
    		else {
    			$errorMessage = "Error logging on";
    		}
    
    	mysql_close($db_handle);
    
    	}
    
    	else {
    		$errorMessage = "Error logging on";
    	}

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    when i enter the user 'xyz' then it is redirecting me to the login page.....

  3. #3
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    the following variables don't appear to be defined in the code you showed us:

    $uname (not defined before it's used, I mean)
    $pword (not defined before it's used, I mean)
    $db_found (although I assume this one is defined somewhere, since you say part of your code is executing)
    $db_handle

    Beyond that, please be more specific about your problem. What have you tried? Do you have error reporting turned on? Have you checked your query and/or the actual database to be sure they hold the values you think they do? (Also, I have absolutely no idea what quote_smart() does.)

  4. #4
    echo out $num_rows and $uname just before their related if statements to see what values are being stored.

  5. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    11
    this is a part of my execution....
    i got no errors .....but the problem is that when i enter user name xyz the i was redirected to the abc.php.....
    so this is the problem.....

  6. #6
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,623
    Does it make any difference if you call "exit()" after each header() call?

    Oh, nevermind on that, probably not. But you could try it.

    You need to put in some debugging echo statements, like spufi suggested. You also might try a tracking variable in the query string on those header calls.
    PHP Code:
    if ($db_found) {

            
    $uname quote_smart($uname$db_handle);
            
    $pword quote_smart($pword$db_handle);

            
    $SQL "SELECT * FROM login WHERE username = $uname AND password = $pword";
            
    $result mysql_query($SQL);
            
    $num_rows mysql_num_rows($result);


            if (
    $result) {
                if (
    $num_rows 0) {
                    if (
    $uname =='abc')
                    {
                    
    session_start();
                    
    $_SESSION['login'] = "1";
                    
    header ("Location: welcomeabc.php?uname=$uname");
                    }
                    else if(
    $uname =='xyz')
                    {
                    
    session_start();
                    
    $_SESSION['login'] = "1";
                    
    header ("Location: welcomexyz.php?uname=$uname");
                        
                    }
                    else
                    {
    session_start();
                    
    $_SESSION['login'] = "1";
                    
    header ("Location: login.php");
                        }
                }
                else {
                    
    //$errorMessage = "Invalid Login";
                    //session_start();
                    //$_SESSION['login'] = '';

                    
    session_start();
                    
    $_SESSION['login'] = "";
                    
    header ("Location: login.php");
                }    
            }
            else {
                
    $errorMessage "Error logging on";
            }

        
    mysql_close($db_handle);

        }

        else {
            
    $errorMessage "Error logging on";
        } 
    Then you can have "welcomeabc.php" and its sister tell you just what that variable is. Actually, you could probably just read it in the QS.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  7. #7
    Unrelated but you could move this code to after the if numrows statement since you know it will be used for any of the options that come within the following if/else statements. The only thing you are really checking for is what header to send.

    PHP Code:
    session_start();
    $_SESSION['login'] = "1"
    It makes the if/else look like this.

    PHP Code:
    session_start();
    $_SESSION['login'] = "1";
    if (
    $uname =='abc')                
        
    header ("Location: welcomeabc.php?uname=$uname");
    else if(
    $uname =='xyz')
        
    header ("Location: welcomexyz.php?uname=$uname");
    else
        
    header ("Location: login.php"); 
    Again, put in echo statements to track what values are being put where to see why the values are what they are.
    Last edited by spufi; 04-16-2012 at 08:15 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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