After inserting the record in the emp table, it is necessary to create a file in PHP that can help the user to update the record in emp table...
To update the record in the emp table, we will need to create form design to get the value by the user as follows........
editemp.html
<html>
<head>
</head>
<body>
<form action=editemp.php method=POST>
Enter no<input type=text name=n>
<input type=submit value=EDIT>
</form>
</body>
</html>
The above code will create the form to get the value from the user...Now to check if the record exists or not, we will need to create PHP file......If record not found then we have to display the message like "Record not found"......If record is found, then we will display the file in the editable format so that the user can edit the record in any fields as per his/her choice.......To perform this function, we will need to write the following code......
editemp.php
<?php
$no=$_POST['n'];
$cn=mysql_connect("localhost","root") or die(mysql_error());
$db=mysql_select_db("student",$cn) or die(mysql_error());
$row=mysql_query("select * from emp where id=$no",$cn) or die(mysql_error());
$n=mysql_num_rows($row);
if($n<=0)
{
echo "Record not found";
exit;
}
else
{
$data=mysql_fetch_row($row);
echo "<form action=updemp.php method=POST>";
echo "No<input type=text value=$data[0] name=id>";
echo "<br> Name<input type=text value=$data[1] name=nm>";
if($data[2]=="comp")
{
echo "<br> Dept<select name='dept'><option value=$data[2]>Computer
<option value='mgmt'>Management
<option value='comm'>Commerce
</select>";
}
else if($data[2]=="mgmt")
{
echo "<br> Dept<select name='dept'><option value=$data[2]>Management
<option value='comp'>Computer
<option value='comm'>Commerce
</select>";
}
else if($data[2]=="comm")
{
echo "<br> Dept<select name='dept'><option value=$data[2]>Commerce
<option value='comp'>Computer
<option value='mgmt'>Management
</select>";
}
echo "<br>Salary <input type=text name=sal value=$data[3]>";
echo "<input type=submit value=UPDATE>";
echo "<br><input type=hidden value=$no name=oldno>";
echo "</form>";
}
?>
This will display the record in the editable form format if the record is found ...........In this form, the user will edit the record in any fields.....After that the record will be needed to update in the database....To do this, we will need to write the following code:
updemp.php
<?php
$nid=$_POST['id'];
$nnm=$_POST['nm'];
$ndep=$_POST['dept'];
$nsal=$_POST['sal'];
$oid=$_POST['oldno'];
if($ndep=="comp")
{
$ta=$nsal*0.10;
$da=$nsal*1.10;
$hra=$nsal*0.25;
}
else if($ndep=="mgmt")
{
$ta=$nsal*0.12;
$da=$nsal*1.05;
$hra=$nsal*0.20;
}
else if($ndep=="comm")
{
$ta=$sal*0.09;
$da=$sal*1.11;
$hra=$sal*0.23;
}
$pf=$nsal*0.10;
$net=($nsal+$ta+$da+$hra)-$pf;
$cn=mysql_connect("localhost","root") or die(mysql_error());
$db=mysql_select_db("student",$cn) or die(mysql_error());
$q=mysql_query("update emp set id=$nid,name='$nnm',dept='$ndep',sal=$nsal,ta=$ta,da=$da,hra=$hra,pf=$pf,net=$net where id=$oid",$cn) or die(mysql_error());
echo $q." Record Updated";
?>
Please note that proper syntax must be written with the same field name that are in the database, otherwise can result into error.....
To update the record in the emp table, we will need to create form design to get the value by the user as follows........
editemp.html
<html>
<head>
</head>
<body>
<form action=editemp.php method=POST>
Enter no<input type=text name=n>
<input type=submit value=EDIT>
</form>
</body>
</html>
The above code will create the form to get the value from the user...Now to check if the record exists or not, we will need to create PHP file......If record not found then we have to display the message like "Record not found"......If record is found, then we will display the file in the editable format so that the user can edit the record in any fields as per his/her choice.......To perform this function, we will need to write the following code......
editemp.php
<?php
$no=$_POST['n'];
$cn=mysql_connect("localhost","root") or die(mysql_error());
$db=mysql_select_db("student",$cn) or die(mysql_error());
$row=mysql_query("select * from emp where id=$no",$cn) or die(mysql_error());
$n=mysql_num_rows($row);
if($n<=0)
{
echo "Record not found";
exit;
}
else
{
$data=mysql_fetch_row($row);
echo "<form action=updemp.php method=POST>";
echo "No<input type=text value=$data[0] name=id>";
echo "<br> Name<input type=text value=$data[1] name=nm>";
if($data[2]=="comp")
{
echo "<br> Dept<select name='dept'><option value=$data[2]>Computer
<option value='mgmt'>Management
<option value='comm'>Commerce
</select>";
}
else if($data[2]=="mgmt")
{
echo "<br> Dept<select name='dept'><option value=$data[2]>Management
<option value='comp'>Computer
<option value='comm'>Commerce
</select>";
}
else if($data[2]=="comm")
{
echo "<br> Dept<select name='dept'><option value=$data[2]>Commerce
<option value='comp'>Computer
<option value='mgmt'>Management
</select>";
}
echo "<br>Salary <input type=text name=sal value=$data[3]>";
echo "<input type=submit value=UPDATE>";
echo "<br><input type=hidden value=$no name=oldno>";
echo "</form>";
}
?>
This will display the record in the editable form format if the record is found ...........In this form, the user will edit the record in any fields.....After that the record will be needed to update in the database....To do this, we will need to write the following code:
updemp.php
<?php
$nid=$_POST['id'];
$nnm=$_POST['nm'];
$ndep=$_POST['dept'];
$nsal=$_POST['sal'];
$oid=$_POST['oldno'];
if($ndep=="comp")
{
$ta=$nsal*0.10;
$da=$nsal*1.10;
$hra=$nsal*0.25;
}
else if($ndep=="mgmt")
{
$ta=$nsal*0.12;
$da=$nsal*1.05;
$hra=$nsal*0.20;
}
else if($ndep=="comm")
{
$ta=$sal*0.09;
$da=$sal*1.11;
$hra=$sal*0.23;
}
$pf=$nsal*0.10;
$net=($nsal+$ta+$da+$hra)-$pf;
$cn=mysql_connect("localhost","root") or die(mysql_error());
$db=mysql_select_db("student",$cn) or die(mysql_error());
$q=mysql_query("update emp set id=$nid,name='$nnm',dept='$ndep',sal=$nsal,ta=$ta,da=$da,hra=$hra,pf=$pf,net=$net where id=$oid",$cn) or die(mysql_error());
echo $q." Record Updated";
?>
Please note that proper syntax must be written with the same field name that are in the database, otherwise can result into error.....
No comments:
Post a Comment