Results 1 to 2 of 2

Thread: Salary update

  1. #1
    Join Date
    Jul 2011
    Posts
    58
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Salary update

    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-
    Code:
    <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 -
    Code:
    <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-
    Code:
    <?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
    Last edited by dcr33; 12-14-2011 at 08:36 AM.

  2. #2
    Join Date
    Oct 2008
    Posts
    60
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default

    emp_id,ename,salary

    emp_select.php
    PHP Code:
    <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
    PHP Code:
    <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
    PHP Code:
    $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.

Bookmarks

Posting Permissions

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