1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

my if statement did not work it always fall in else statement

Discussion in 'PHP' started by rhodarose, Jul 8, 2010.

  1. #1
    I created a login webpage that consist of username and department. I want that when i input my username and i choose my department and i submit it automatically connect to the department where i belong. but sad to say i think my if statement was not work because when i submit my username and department it falls in the same form.

    this is my code:

    form.php

    
    <html>
    <body background="bgroundv03.png" bgproperties="fixed">
    <form id="form1" name="form1" method="post" action="loginv07.php" >
     
      <div id="Layer12">
     
        <select name="department">
          <option>Choose your Department</option>
          <option value="<?php $_POST['Accounting']?>">Accounting</option>
          <option value="<?php $_POST['Engineering']?>">Engineering</option>
          <option value="<?php $_POST['Finishing_Goods']?>">Finishing Goods</option>
          <option value="<?php $_POST['HRAD']?>">HRAD</option>
          <option value="<?php $_POST['MIS']?>">MIS</option>
          <option value="<?php $_POST['Packaging_and_Design']?>">Packaging and Design</option>
          <option value="<?php $_POST['Production']?>">Production</option>
          <option value="<?php $_POST['Purchasing_Logistic']?>">Purchasing and Logistics</option>
          <option value="<?php $_POST['QA_and_Technical']?>">QA and Technical</option>
          <option value="<?php $_POST['Supply_Chain']?>">Supply Chain</option>
        </select>
      </div>
      <div id="Layer3">
        <p><img src="subframev02.png" width="237" height="56" /></p>
        <div id="Layer4">
          <input name="username" type="text" size="30" />
        </div>
      </div>
      <div id="Layer10"><img src="subframev02.png" width="200" height="44" /></div>
    
      <div id="Layer11"><img src="userv01.png" width="119" height="60" /></div>
      <div id="Layer7"><img src="subframev02.png" width="76" height="50" /></div>
    
      <div id="Layer8">
        <input type="submit" name="Submit" value="Submit" />
      </div>
    </form>
    </body>
    </html>
    
    Code (markup):
    my code in con.php
    
    <?php
    $host="localhost";
    $username="root";
    $password="";
    $db_name="dspi";
    $tbl_name="tbllogin";
    
    
    mysql_connect("$host", "$username", "$password") or die("Cannot connect to server");
    mysql_select_db("$db_name")or die("Cannot select DB");  
     
    $department = mysql_real_escape_string($_POST['department']);  
    $username = mysql_real_escape_string($_POST['username']);
    
    $sql=mysql_query("SELECT * FROM tbllogin WHERE Department = 'department' and Username = 'username'") or die(mysql_error());  
    $row = mysql_fetch_assoc($sql); 
    
        if($row['Username']=='$username' && $row['Department']=='Accounting')
    		{
    			header('location:accounting.php');
    		}
    	elseif($row['Username']=='$username' && $row['Department']=='Engineering')
    		{
    			header('location:engineering.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='Finishing_Goods')
    		{
    			header('location:finishing_goods.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='HRAD')
    		{
    			header('location:HRAD.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='MIS')
    		{
    			header('location:MIS.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='Packaging_and_Design')
    		{
    			header('location:packaging_design.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='Production')
    		{
    			header('location:production.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='Purchasing_Logistic')
    		{
    			header('location:purchasing_logistic.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='QA_and_Technical')
    		{
    			header('location:QA_technical.php');
    		}
        elseif($row['Username']=='$username' && $row['Department']=='Supply_Chain')
    		{
    			header('location:supply_chain.php');
    		}
     	else 
     	{  
         header('location:dspiv07.php');  
     	} 
    ?>
    
    Code (markup):

    SEMrush
     
    rhodarose, Jul 8, 2010 IP
    SEMrush
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    looks like your problem is with ones like:
      <option value="<?php $_POST['Accounting']?>">
    PHP:
    needs an echo for the $_POST value
     
    themullet, Jul 9, 2010 IP
  3. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Your query ensures that the value of $row['Department'] will always be the value 'department'.

    
    $sql=mysql_query("SELECT * FROM tbllogin WHERE Department = 'department' and Username = 'username'") or die(mysql_error());
    
    PHP:
    Your if statements never look for that value, they always check for values like 'Production', 'Accounting', etc. Because of the WHERE condition in your SQL the value of $row['Department'] can never be anything but this string: "department".

    Maybe you want that line to be:

    
    $sql=mysql_query("SELECT * FROM tbllogin WHERE Department = '".$department."' and Username = '".$username."'") or die(mysql_error());
    
    PHP:
     
    plog, Jul 9, 2010 IP