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

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>

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');  
 	} 
?>

Carefully look the value of the options . Do you get what I mean ?


<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>

Just use firebug and see whether the values are there :slight_smile: . It will not be , you are trying to get from a post value .

So here when the form is submitted how come the department gets the value ?

Use


echo "<pre>";
print_r( $_POST );
echo "</pre>";

and see you are getting the correct values in post . You can also use var_dump as Cups says .

Thanks


<form id="form1" name="form1" method="post" action="loginv07.php" >
<select name="department">
      <option>Choose your Department</option>
      <option>Accounting</option>
      <option>Engineering</option>
</select>
<input name="username" type="text" size="30" />
<input type=submit />
</form>

loginv07.php


// restore redacted parts 


$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); 
var_dump( $row ) ; // take a look at the results
// then do a comparison