我有一个HTML页面,就像一个"离开申请表格"其中有一个名为"接受"
的按钮我已经编写了下面的PHP代码,当我点击该接受按钮时,需要执行该代码。
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("leave", $con);
$leaveType=$_POST['leavename'];
$totalDays=$_POST['days'];
$EId=$_POST[empid];
$sql="SELECT
LFA, SickLeave, TransferLeave, HolidayLeave, AnnualLeave, UnpaidLeave
FROM leaverecords
WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
$result=mysql_query($sql);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$lfa = $row['LFA'];
$sleave = $row['SickLeave'];
$tleave = $row['TransferLeave'];
$hleave = $row['HolidayLeave'];
$aleave = $row['AnnualLeave'];
$unpaidleave = $row['UnpaidLeave'];
}
if($leaveType=="LFA")
{
if( $lfa>0 && $lfa>=$totalDays)
{
$newlfa = $lfa - $totalDays;
$sql="UPDATE leaverecords SET LFA = '$newlfa' WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
else
echo "Leave not available!";
}
elseif($leaveType=="Sick Leave")
{
$newsleave = $sleave + $totalDays;
$sql="UPDATE leaverecords SET SickLeave = '$newsleave' WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
elseif( $leaveType=="Transfer Leave")
{
if($tleave!=5)
{
$newtleave = $tleave + $totalDays;
$sql="UPDATE leaverecords SET TransferLeave = '$newtleave' WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
else
echo "Leave not available!";
}
elseif($leaveType=="Holiday Leave")
{
$newhleave = $hleave + $totalDays;
$sql="UPDATE leaverecords SET HolidayLeave = '$newhleave' WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
elseif($leaveType=="Annual Leave")
{
if($aleave>0 && $aleave>=$totalDays)
{
$newaleave = $aleave - $totalDays;
$sql="UPDATE leaverecords SET AnnualLeave = '$newaleave' WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
else
echo "Leave not available!";
}
elseif($leaveType=="Unpaid Leave")
{
if($unpaidleave<90)
{
$newunpaidleave = $unpaidleave + $totalDays;
$sql="UPDATE leaverecords SET UnpaidLeave = '$newunpaidleave' WHERE empid='$EId'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
}
else
echo "Leave not available!";
}
?>
点击它后,如何使用按钮使PHP代码运行?
答案 0 :(得分:1)
<form method="post" action="url_to_that_script.php">
<input value="Accept" type="submit">
<!-- other fields -->
</form>