table 中的记录被删除后如何重定向到 edit/delete 数据页
how te redirect to edit/delete data page after a record from table was deleted
我从数据库中检索一个列表,其中包含一些数据的 ID 和标题,并对每个数据进行编辑和删除 link/button
在我点击删除任何数据后,它可以正常工作,但我无法让它将我重定向到 edit/delete 页面。这是 table:
的代码
<table>
<tr>
<td>ID</td>
<td>Nume poveste</td>
<td>edit</td>
<td>delete</td>
</tr>
<?php
foreach($results_stories as $data) {
echo "<tr>";
echo "<td>" . $data->id_story . "</td>";
echo "<td>" . $data->title . "</td>";
echo "<td><a href='edit_data.php?id=" . $data->id_story . "'>edit</a></td>";
echo "<td><a href='delete_row.php?id=" . $data->id_story . "'>del</a></td>";
echo "</tr>";
}
?>
这里是 delete.php 的代码:
try {
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$query = $conn->query("DELETE FROM stories WHERE id_story = '$_GET[id]'");
$stmt = $pdo->prepare($sql);
// use exec() because no results are returned
$stmt->bindParam(':id_story', $data->id_story, PDO::PARAM_INT);
if($stmt->execute()) {
echo "Record deleted successfully";
header("Location: http://localhost/auth2/edit.php");
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
有什么想法吗?
要正确使用准备好的语句,您应该:
$stmt = $conn->prepare("DELETE FROM stories WHERE id_story = ?");
if($stmt->execute(array($_GET['id']))) {
?
是值的占位符。 PDO
driver 将其添加到查询中并根据需要引用它。
要解决 header 问题,我会这样做:
header("Location: http://localhost/auth2/edit.php?response=Record+deleted+successfully");
然后 edit.php
:
if(!empty($_GET['response'])) {
echo htmlspecialchars($_GET['response'], ENT_QUOTES);
}
我从数据库中检索一个列表,其中包含一些数据的 ID 和标题,并对每个数据进行编辑和删除 link/button
在我点击删除任何数据后,它可以正常工作,但我无法让它将我重定向到 edit/delete 页面。这是 table:
的代码<table>
<tr>
<td>ID</td>
<td>Nume poveste</td>
<td>edit</td>
<td>delete</td>
</tr>
<?php
foreach($results_stories as $data) {
echo "<tr>";
echo "<td>" . $data->id_story . "</td>";
echo "<td>" . $data->title . "</td>";
echo "<td><a href='edit_data.php?id=" . $data->id_story . "'>edit</a></td>";
echo "<td><a href='delete_row.php?id=" . $data->id_story . "'>del</a></td>";
echo "</tr>";
}
?>
这里是 delete.php 的代码:
try {
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$query = $conn->query("DELETE FROM stories WHERE id_story = '$_GET[id]'");
$stmt = $pdo->prepare($sql);
// use exec() because no results are returned
$stmt->bindParam(':id_story', $data->id_story, PDO::PARAM_INT);
if($stmt->execute()) {
echo "Record deleted successfully";
header("Location: http://localhost/auth2/edit.php");
}
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
有什么想法吗?
要正确使用准备好的语句,您应该:
$stmt = $conn->prepare("DELETE FROM stories WHERE id_story = ?");
if($stmt->execute(array($_GET['id']))) {
?
是值的占位符。 PDO
driver 将其添加到查询中并根据需要引用它。
要解决 header 问题,我会这样做:
header("Location: http://localhost/auth2/edit.php?response=Record+deleted+successfully");
然后 edit.php
:
if(!empty($_GET['response'])) {
echo htmlspecialchars($_GET['response'], ENT_QUOTES);
}