Else is not working

  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

In my following code if pare and elseif part is perfectly working. But at last the else part is not working. If I write it elseif and give a condition it is working. I need to use the java script because of the popup message. You people just help me to run the else part of the checking of designation.

Code: [ Select ]
<?php
session_start();
$name=$_POST["name"];
$_SESSION['name']=$name;
$pass=$_POST["pass"];
include('db.php');
if($link==TRUE)
{
  if($dbsel==TRUE)
  {
  
  $query="select designation from registration where password='$pass' and name='$name'";
  $result=@mysql_query($query);
    
      while($row=mysql_fetch_array($result))
      {
        
  
        if($row['designation']=="admin")
        {
              
        ?>
        <script type="text/javascript">
window.location = "index_admin.php";
        </script>
        <?php
        }
        elseif($row['designation']=="customer")
        {
      
        ?>
        <script type="text/javascript">
window.location = "index_user.php";
        </script>
        <?php
        }
        //elseif($row['designation']=="xxx")
        else
        {
        echo "joy";
    echo "<script>alert('You have entered the information incorrectly, please try again or register as a new user')</script>";
      ?>
      <script type="text/javascript">
           window.location = "login.php";
           </script>
      <?php
        exit();
        }
      }
    
    
    }
  
  else
  {
  echo "Database not exist";
  }
}
else
{
echo "Connection failed";
}

?>
  1. <?php
  2. session_start();
  3. $name=$_POST["name"];
  4. $_SESSION['name']=$name;
  5. $pass=$_POST["pass"];
  6. include('db.php');
  7. if($link==TRUE)
  8. {
  9.   if($dbsel==TRUE)
  10.   {
  11.   
  12.   $query="select designation from registration where password='$pass' and name='$name'";
  13.   $result=@mysql_query($query);
  14.     
  15.       while($row=mysql_fetch_array($result))
  16.       {
  17.         
  18.   
  19.         if($row['designation']=="admin")
  20.         {
  21.               
  22.         ?>
  23.         <script type="text/javascript">
  24. window.location = "index_admin.php";
  25.         </script>
  26.         <?php
  27.         }
  28.         elseif($row['designation']=="customer")
  29.         {
  30.       
  31.         ?>
  32.         <script type="text/javascript">
  33. window.location = "index_user.php";
  34.         </script>
  35.         <?php
  36.         }
  37.         //elseif($row['designation']=="xxx")
  38.         else
  39.         {
  40.         echo "joy";
  41.     echo "<script>alert('You have entered the information incorrectly, please try again or register as a new user')</script>";
  42.       ?>
  43.       <script type="text/javascript">
  44.            window.location = "login.php";
  45.            </script>
  46.       <?php
  47.         exit();
  48.         }
  49.       }
  50.     
  51.     
  52.     }
  53.   
  54.   else
  55.   {
  56.   echo "Database not exist";
  57.   }
  58. }
  59. else
  60. {
  61. echo "Connection failed";
  62. }
  63. ?>
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8411
  • Loc: USA

Post 3+ Months Ago

Have you tried to use switch?
PHP Code: [ Select ]
switch($row['designation'])
{
    case "admin":
        // do stuff here
        break;
    case "customer":
        // do stuff here
        break;
    case default:
        // The 'else' code
        break;
}
  1. switch($row['designation'])
  2. {
  3.     case "admin":
  4.         // do stuff here
  5.         break;
  6.     case "customer":
  7.         // do stuff here
  8.         break;
  9.     case default:
  10.         // The 'else' code
  11.         break;
  12. }
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

Thanks dear. I will try it then let you know. But is not there any way to make work the last else where if any one put wrong uname, pass will get back to the login page.
  • joy1986joy
  • Novice
  • Novice
  • User avatar
  • Posts: 27

Post 3+ Months Ago

I solved the problem at last. There the else is in wrong place. So when somebody enter a wrong user name or password it will not enter into the while loop, So there is no question of entering into the "if else" loop. So first it have to check that is there any row selected or not. If not selected then It should declare the info is wrong. So now its working well and good. Thanks Bogey for trying to help.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: Liamw411 and 135 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.