简体   繁体   中英

Run delete PHP/SQL only when checkbox is ticked

I have the following scenario:

User can see all their logged entries, but wants to delete a single one. I have the following php part, but this deletes everything from the table (which is fine), but disregards if the checkbox is ticked or not. It should only delete when the checkbox is ticked:

    // 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";
     }   

     ?>

You execute your query two times.

in $result = mysqli_query($conn, $query); (the one causing your unexpected behavior) and $conn->query($query) == TRUE;

Just delete $result = mysqli_query($conn, $query); and you should be fine with it.

For sure you should improve your code anyway, but this one line is causing your actual problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM