删除表中的记录后如何重定向编辑/删除数据页

时间:2016-06-18 14:43:17

标签: php mysql pdo sql-delete

我从数据库中检索一个包含某些数据的ID和标题的列表,并为每个数据创建一个编辑和删除链接/按钮

我点击删除任何数据后,但我无法让它重定向到编辑/删除页面。这是表格的代码:

<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();
}  

任何想法?

1 个答案:

答案 0 :(得分:2)

要正确使用准备好的陈述,您应该:

$stmt = $conn->prepare("DELETE FROM stories WHERE id_story = ?");
if($stmt->execute(array($_GET['id']))) {

?是值的占位符。 PDO驱动程序将其添加到查询中并根据需要引用它。

要解决标题问题,请执行以下操作:

header("Location: http://localhost/auth2/edit.php?response=Record+deleted+successfully");

然后在edit.php

if(!empty($_GET['response'])) {
    echo htmlspecialchars($_GET['response'], ENT_QUOTES);
}