仅在选中复选框时运行删除PHP / SQL

时间:2018-12-05 14:25:18

标签: php mysql forms input

我有以下情况:

用户可以看到他们所有已记录的条目,但想要删除一个条目。我有以下php部分,但这会删除表中的所有内容(这很好),但是忽略了复选框是否被选中。仅应在选中该复选框时删除它:

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    } 
    $query = "DELETE FROM `trips` WHERE `id` > 1" ; 
    // this changes later to user specific instances - only example for now

    $result = mysqli_query($conn, $query);
    if(isset($_POST['deletecheck']) && $_POST['deletecheck'] == 'Yes' )
    {
     $conn->query($query) == TRUE;
      echo "deleted";
    }
    else
         {
     echo "ERROR: tick the checkbox and press delete to delete the trip";
     }   

     ?>

1 个答案:

答案 0 :(得分:1)

您执行两次查询。

$result = mysqli_query($conn, $query);(导致您意外行为的那个)和$conn->query($query) == TRUE;

只需删除$result = mysqli_query($conn, $query);,就可以了。

可以肯定的是,您还是应该改进代码,但是这一行引起了您的实际问题