Php 更新数据库值后删除按钮
我有一个页面,用户可以查看他们提交的请假申请,也可以取消申请,如下所示: 我想在取消或单击“取消”按钮后取消它。所以我在执行代码中添加了一个remove按钮脚本。显然,这是错误的。在我将脚本添加到执行代码中之前,一切都很顺利 这是我的表格: HTML+PHPPhp 更新数据库值后删除按钮,php,jquery,html,Php,Jquery,Html,我有一个页面,用户可以查看他们提交的请假申请,也可以取消申请,如下所示: 我想在取消或单击“取消”按钮后取消它。所以我在执行代码中添加了一个remove按钮脚本。显然,这是错误的。在我将脚本添加到执行代码中之前,一切都很顺利 这是我的表格: HTML+PHP <div class="container"> <div class="page-header"> <h3>My Leaves</h3> <di
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
</form>
</td>
</tr>
<?php }?>
</table>
<a href="employee_panel.php">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</a>
</div>
</div>
</div>
我的叶子
员工姓名
电话
电子邮件
从…起
到
理由
类型
地位
if语句中附带的cancelled按钮,如果记录被标记为cancelled,则按钮将不再显示
您可以尝试此更新代码:
<?php
if(isset($_POST['cancelled'])){
$msg = "Cancelled";
$status=$_POST['cancelled'];
echo "<script>
$(document).ready(function(){
$(".removebtn").click(function(){
$(this).remove();
});
});
</script>";
}
$leaveno=$_POST['leaveno'];
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con, 'companydb');
$sql = "UPDATE leaves SET status = '$status' WHERE leaveno = '$leaveno'";
if(mysqli_query($con, $sql))header("refresh:1; url=view-leave-emp.php?msg=$msg");
else var_dump(mysqli_error($con));
?>
<div class="container">
<div class="page-header">
<h3>My Leaves</h3>
<div class="table-responsive">
<table class="table">
<tr>
<th>Employee Name</th>
<th>Phone</th>
<th>Email</th>
<th>From</th>
<th>To</th>
<th>Reason</th>
<th>Type</th>
<th>Status</th>
</tr>
<?php
include ('database.php');
$result = $database->prepare ("SELECT leaves.* FROM leaves INNER JOIN employee ON employee.id = leaves.user_id WHERE employee.username = '".$_SESSION["VALID_USER_ID"]."'");
$result ->execute();
for ($count=0; $row_message = $result ->fetch(); $count++){
?>
<tr>
<td><?php echo $row_message['firstname']." " .$row_message['lastname']; ?></td>
<td><?php echo $row_message['phone']; ?></td>
<td><?php echo $row_message['email']; ?></td>
<td><?php echo $row_message['fromdate']; ?></td>
<td><?php echo $row_message['todate']; ?></td>
<td><?php echo $row_message['reason']; ?></td>
<td><?php echo $row_message['type']; ?></td>
<td><?php echo $row_message['status']; ?></td>
<td>
<form method="post" action="update-leave-status-emp.php">
<input type="hidden" name="leaveno" value="<?php echo $row_message['leaveno']; ?>" />
<?php if( $row_message['status'] != "Cancelled" ) {
<input type="submit" value="Cancelled" class="removebtn" name="cancelled"></input>
<?php } ?>
</form>
</td>
</tr>
<?php }?>
</table>
<a href="employee_panel.php">
<button type="button" class="btn btn-primary"><i class="glyphicon glyphicon-arrow-left"></i> Back</button>
</a>
</div>
</div>
</div>
我的叶子
员工姓名
电话
电子邮件
从…起
到
理由
类型
地位
警告:当使用mysqli
时,您应该使用和将用户数据添加到查询中。不要使用字符串插值或串联来完成此操作,因为您已经创建了严重的错误。切勿将$\u POST
、$\u GET
或任何用户数据直接放入查询中,如果有人试图利用您的错误,这可能会非常有害。注意:mysqli
的面向对象界面明显不那么冗长,使代码更易于阅读和审核,并且不容易与过时的mysql\u查询
接口混淆。在你对程序性风格投入太多之前,值得换一种。示例:$db=new mysqli(…)
和$db->prepare(“…”)
过程接口是引入mysqli
API时PHP4时代的产物,不应在新代码中使用。由于无法使用PHP更改allready呈现的页面,因此需要使用Javascript。此外,您的代码非常容易受到SQL注入的影响。小心点。@tadman我意识到了这一点,一旦我解决了这个问题,我会修改它。现在在你写它的时候修改它。这样的编码是完全鲁莽的,必须不惜一切代价避免。如果你忘记了这一点,如果你养成一种习惯,以后再考虑安全问题,那么最终肯定会发生,你会被烧死。正确操作不仅安全,还可以节省大量时间来追踪与转义问题相关的恼人且完全可以预防的bug。顺便说一句,您的代码容易受到sql注入的攻击。你可能还想修正你准备好的陈述来防止这种情况。