我无法使用php从mysql中删除记录
我一直在创建一个staff directory web应用程序来学习和测试我所学的内容,但是我在删除mysql数据库中的记录时遇到了问题,其中staff_id=staff_id来自上一页,它会重新确认记录并填充字段,但在我选择yes时不会删除记录 任何帮助或指导都会很好:D我无法使用php从mysql中删除记录,php,mysql,sql,Php,Mysql,Sql,我一直在创建一个staff directory web应用程序来学习和测试我所学的内容,但是我在删除mysql数据库中的记录时遇到了问题,其中staff_id=staff_id来自上一页,它会重新确认记录并填充字段,但在我选择yes时不会删除记录 任何帮助或指导都会很好:D <?php # display all php errors error_reporting(E_ALL); ini_set('display_errors', 1); # include dbConnection
<?php
# display all php errors
error_reporting(E_ALL);
ini_set('display_errors', 1);
# include dbConnection details
require '/includes/dbconn.php';
# if $staff_id is not empty, GET the id
if (!empty($_GET['staff_id'])) {
$staff_id = $_GET['staff_id'];
}
# check if data has been posted
if (!empty($_POST)) {
# modify the variable request from GET to POST
$staff_id = $_POST['staff_id'];
# delete data from joke table
$sql = "DELETE FROM staff WHERE staff_id = :staff_id";
$stmt = $DB_con->prepare($sql);
$stmt->bindValue(':staff_id', $staff_id);
$stmt->execute();
header("Location: Admin_View_Employee.php");
exit();
# display current record
} else {
$sql = "SELECT staff_id, forename, surname, job_role, joined, manager_id, extension, mobile, email, background_info, qualifications, achievements, username, password, level, dept_id
FROM staff
WHERE staff_id = :staff_id";
$stmt = $DB_con->prepare($sql);
$stmt->bindValue(':staff_id', $staff_id);
$stmt->execute();
$data = $stmt->fetch();
$staff_id = $data['staff_id'];
$forename = $data['forename'];
$surname = $data['surname'];
$job_role = $data['job_role'];
$manager_id = $data['manager_id'];
$joined = $data['joined'];
$extension = $data['extension'];
$mobile = $data['mobile'];
$email = $data['email'];
$background_info = $data['background_info'];
$qualifications = $data['qualifications'];
$achievements = $data['achievements'];
$dept_id = $data['dept_id'];
}
# if data is not found
if(!$data)
{
header("Location: Admin_View_Employee.php");
exit();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Update Employee</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<div>
<table width="100%" border="0">
<tbody>
<tr>
<td width="25%"><img src="images/beacon_logo.png" width="240" height="168" align="left"></td>
<td width="50%"><h1 style="text-align: center" border="0">Update Employee</h1></td>
<td width="25%"> </td>
</tr>
</tbody>
</table>
</div>
<?php include('Admin_Nav.php'); ?>
</tbody>
</table></p>
<div>
<form action="Admin_Delete_Employee.php?staff_id=<?php echo $staff_id ?>" method="post">
<input type="hidden" name="id" value="<?php echo $staff_id; ?>"/>
<p>Are you sure to delete this record?</p>
<table width="574">
<tr>
<th width="131">Staff ID</th>
<td width="84"><?php echo $staff_id;?></td>
</tr>
<tr>
<th>Forename</th>
<td><?php echo $forename;?></td>
</tr>
<tr>
<th>Surname</th>
<td><?php echo $surname;?></td>
</tr>
<tr>
<th>Job Role</th>
<td><?php echo $job_role;?></td>
</tr>
</tr>
</table>
<div>
<button type="submit">[Yes]</button>
<a href="Admin_View_Employee.php">[No]</a>
</div>
</form>
</div>
</body>
</html>
依照
因此,请使用bindParam
将表单方法从post更改为get 能这么简单吗 更改:
$stmt->bindValue(':staff_id', $staff_id);
到
如果staff_id是一个整数,这将起作用。name=id!=$_发布['staff_id']…或将其用作$\u GET,因为您也以这种方式发送它?staff_id=您只需要取消另一个覆盖。您收到了什么错误消息?您遇到问题的确切代码是什么?为什么这会产生不同?变量被覆盖为空。您可以检查这种情况,我知道它的作用。但是请看代码$职员id=$职位['staff_id'];看看这个帖子。。。为了确保你的帖子不是空的,它已经是一个GET。根据您发布的字符串?staff_id=。它在删除中覆盖。这是一个打字错误,应该投票决定是否结束。大致上问题是你说了什么,但真正的问题只是$staff\u id=$\u POST['staff\u id'];这将覆盖OP已发送的GET值。如果OP更改方法,则其他脚本值需要更改!空的$u POST{..等等。在这种情况下,它应该是id而不是staff\u id,对吗?@chris85Yea,这也行…正如我读到的。OP似乎已经消失了。嗨,克里斯,我只是根据你说的调整代码,并将其更改为$staff\u id=$u POST['id'];这似乎已经解决了问题。感谢大家的帮助。这是一个输入错误,应该投票关闭。该变量在删除条件中被覆盖。staff_id是一个整数,我添加了PDO::PARAM_INT;但该记录仍显示在数据库中。
<form action="Admin_Delete_Employee.php?staff_id=<?php echo $staff_id ?>" method="get">
$stmt->bindValue(':staff_id', $staff_id);
$stmt->bindValue(':staff_id', $staff_id, PDO::PARAM_INT);