Friday, 4 September 2015

Insert record in Emp table

First of all create the database in MYSQL....Then create emp table with the following fields:
ID              INT                  3
NAME      VARCHAR    34
DEPT        VARCHAR    34
SAL           FLOAT            7
TA             FLOAT            7
DA            FLOAT            7
HRA          FLOAT            7
PF             FLOAT            7
NET          FLOAT            7
Then create the design of the form to insert the record in EMP table as follows:
empins.html

<html>
    <head><title>Insert Employee</title>
    </head>
    <body>
        <form action=empins.php method=POST>
        Emp ID<input type=text name=id><br>
        Emp name<input type=text name=nm><br>
        Emp Dept<select name=dept>
                <option value="comp">Computer
                <option value="mgmt">Management
                <option value="comm">Commerce
            </select><br>
        Emp Salary<input type=text name=sal><br>
        <input type=submit value=Insert>
        </form>
    </body>
</html>

Then create .php file to write the code of inserting the record in employee

empins.php

<?php
    $id=$_POST['id'];
    $nm=$_POST['nm'];
    $dep=$_POST['dept'];
    $sal=$_POST['sal'];
    if($dep=="comp")
    {
        $ta=$sal*0.10;
        $da=$sal*1.10;
        $hra=$sal*0.25;
    }
    else if($dep=="mgmt")
    {
        $ta=$sal*0.12;
        $da=$sal*1.05;
        $hra=$sal*0.20;
    }
    else if($dep=="comm")
    {
        $ta=$sal*0.09;
        $da=$sal*1.11;
        $hra=$sal*0.23;
    }
    $pf=$sal*0.10;
    $net=($sal+$ta+$da+$hra)-$pf;
    $cn=mysql_connect("localhost","root") or die(mysql_error());
    $db=mysql_select_db("student",$cn) or die(mysql_error());
    mysql_query("insert into emp values($id,'$nm','$dep',$sal,$ta,$da,$hra,$pf,$net)",$cn) or die(mysql_error());
    echo "Record inserted";
?>
  Please note that the count may vary as per the users' choice

No comments:

Post a Comment