Log in

View Full Version : Salary update



dcr33
12-14-2011, 04:57 AM
I have this program -
To Develop a php application to modify salary of an employee,your application should provide facility to choose an employee from a drop-down list.

I tried with the following code but it cannot change the record in the table "emp" inside the "db1" database. There are 3 pages in total. The table "emp" has 3 fields -emp_id,ename,salary.
Here is the code-
1)emp_select.php-


<html>
<head>
<title></title>
</head>
<body>
<form action="emp_mod.php" method="post">
Choose an employee <select name="emp">
<?php
$host="localhost";
$user="root";
$passwd="";
$con=mysql_connect($host,$user,$passwd);
mysql_select_db('db1',$con);
$sql="select emp_id,ename from emp";
$result=mysql_query($sql,$con);

?>
<input type="submit" value="show">
</form>
</body>
</html>


2)emp_mod.php -

<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<?php
$eid=$_POST['emp'];
$con=mysql_connect('localhost','root','');
mysql_select_db('db1',$con);
$sql="select ename,salary from emp where emp_id='$eid'";
$result=mysql_query($sql,$con);
$rec=mysql_fetch_row($result);
$enm=$rec[0];
$sal=$rec[1];
?>
<form action="emp_upd.php" mehtod="post">
Emp. ID <input type="text" name="eid" value="<?php echo $eid; ?>" readonly="readonly"><br />
Emp. Name: <input type="text" readonly="readonly" name="enm" value="<?php echo $enm;?>"><br />
Mthly Salary: <input type="text" name="sal" value="<?php echo $sal;?>"><br />
<br />
<input type="submit" value="Save">
</form>
</body>
</html>

3)emp_upd.php-

<?php
$eid=$_POST['eid'];
$sal=$_POST['sal'];
$con=mysql_connect('localhost','root','');
mysql_select_db('db1',$con);
$sql="update emp set salary='$sal' where emp_id='$eid'";
if(mysql_query($sql,$con))
{
echo "Record changed<br>";
}
else
{
die('Unable to change salary<br>');
}
?>
Where can be the problem in the above code?
Pls help
Thanks in advance

fobos
12-14-2011, 04:12 PM
emp_id,ename,salary

emp_select.php


<html>
<head>
<title></title>
</head>
<body>
<form action="emp_mod.php" method="post">
Choose an employee
<select name="emp">
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("db1", $con);
$result = mysql_query("SELECT * FROM emp");
while($row = mysql_fetch_array($result)) {
echo "<option value='".$row['emp_id']."'>".$row['ename']."</option>";
}
mysql_close($con);
?>
</select>
<input type="submit" value="show">


emp_mod.php


<html>
<head>
<title>Untitled Document</title>
</head>

<body>
<?php
$emp_id = $_POST['emp'];
echo $emp_id; // Test to see if the emp_id is passed to next page. if so just delete this.
$con = mysql_connect("localhost","root","");
mysql_select_db("db1", $con);
$result = mysql_query("SELECT * FROM emp WHERE emp_id = '$emp_id'");
$row = mysql_fetch_array($result);
?>
<form action="emp_upd.php" mehtod="post">
Emp. ID <input type="text" name="id" value="<?php echo $row['emp_id']; ?>" readonly="readonly"><br />
Emp. Name: <input type="text" readonly="readonly" name="name" value="<?php echo $row['ename']; ?>"><br />
Mthly Salary: <input type="text" name="salary" value="<?php echo $row['salary']; ?>">
<br />
<input type="submit" value="Save">
</form>
</body>
</html>
mysql_close($con);


emp_update.php


$id = $_POST['id'];
$name = $_POST['name'];
$salary = $_POST['salary'];
$con = mysql_connect("localhost","root","");
mysql_select_db("db1", $con);
mysql_query("UPDATE emp SET salary = '$salary' WHERE emp_id = '$id'");
echo "Sucessfuly updated ".$name." 's salary to ".$salary;

mysql_close($con);
?>


I hope this works for you. Im doing this at work, so i apologize if there might be an error. just point it out and i will help you.