从表中删除数据时出错

时间:2018-01-21 17:07:16

标签: php mysql

这是我的脚本delete.php:

<?php
if (isset($_GET['delete'])) {
include_once 'connect.php';

$id = mysqli_real_escape_string($conn, $_GET['delete']);

$sql = mysqli_query($conn, "DELETE FROM tabel WHERE id=$id");
$query_execute = mysqli_query($conn,$sql);
if ($query_execute) {
    header("location: ../task.php?qdel=success");
} else {
    echo mysqli_error($conn);
    header("location: ../task.php?edel=error");
}
mysqli_close($conn);
?>

单击“删除”时,页面不会重定向。我收到这个错误:

  

“您的SQL语法中有错误;请查看与您的MySQL服务器版本对应的手册,以便在第1行的”1“附近使用正确的语法。

但是当我在浏览器中单击时,该行将被删除。我错过了什么?

1 个答案:

答案 0 :(得分:0)

我正在使用此代码...在我的情况下,准备工作正常......

<?php
if (isset($_GET['delete'])) {
   include 'connect.php';
   $id = $_GET['delete'];
   $deleteString = "DELETE FROM penempatan WHERE id = ?";
   $preparedDeleteStmt = mysqli_prepare($conn, $deleteString);
   mysqli_stmt_bind_param($preparedDeleteStmt, 'i', $id);

    if (!mysqli_stmt_execute($preparedDeleteStmt)) {
        mysqli_close($conn);
        die("The system is not available, try again later");
        header("location: ../tugas.php?edel=error");
    } 

    if(mysqli_stmt_store_result($preparedDeleteStmt)) {
        header("location: ../tugas.php?qdel=success");
    }
}
?>