简体   繁体   中英

Delete selected checkbox rows from mysql using php

I am having troubles with deleting the selected checkbox rows from mysql using php code. I tried searching for the same problem here and implementing the logic in my practice project, but it doesn't seem to work. Once I select the checkboxes to delete, it loads the blank page and nothing is deleted.

The first two parts with inserting new values and deleting the row by typing the ID work correctly.

Here's my code.

<?php

if (isset( $_POST["create"])){
    
    $conn= mysqli_connect("localhost","root","" );
    mysqli_select_db($conn, "phonebook_assignement");
    $phonenumber= $_POST["number"];
    $textname= $_POST["name"];
   
    $sql = "INSERT INTO contacts(name, number) VALUES ('$textname', $phonenumber)";
    //exit($sql);
    $rs= mysqli_query($conn,$sql);
   // exit($rs);
  
    if ($rs) {
       echo "Record inserted";
    }
   
    mysqli_close($conn);
    die();
}

elseif (isset($_POST["delete"])){
    $conn= mysqli_connect("localhost","root","" );
    mysqli_select_db($conn, "phonebook_assignement");
    $id= $_POST["id"];
    $delete = "DELETE FROM contacts WHERE id=$id";
    $sqlDelete= mysqli_query($conn, $delete);
enter code here

    if ($sqlDelete){
        echo "Record deleted";
    }

    mysqli_close($conn);    
    die();
}




elseif (isset($_POST["delete"])){
    $conn= mysqli_connect("localhost","root","" );
    mysqli_select_db($conn, "phonebook_assignement");
    $checkbox= $_POST["checkbox"];
    for($i=0;$i<count($checkbox);$i++){

        $del_id = $checkbox[$i];
        $deleteBox = "DELETE FROM contacts WHERE id=$del_id";      
        $boxDelete= mysqli_query($conn, $deleteBox);
    }

    if ($boxDelete){
        echo "Record deleted";
    }

    mysqli_close($conn);    
    die();
}
    
?>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"> 
    <title> Phonebook assignement</title> 
</head>   

<body>    

<?php

$conn= mysqli_connect("localhost","root","" );
mysqli_select_db($conn, "phonebook_assignement");

$result= mysqli_query($conn, "SELECT * FROM contacts");

while ($rw=mysqli_fetch_row($result))
    echo  '<form method="POST" action="dbassignment_testcheckbox.php"><input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $rw["id"]; ?></form>' . "ID: ". $rw[0]. "- Name: ". $rw[1]. "- Phone Number: ". $rw[2]. "<br>";
?>

<br>
<fieldset>
<form method="POST" action="dbassignment_testcheckbox.php" name="createUser">
    
        <label for="name">Insert new name : </label>
        <input type="text" name= "name" id="name">

        <label for="number">Insert new phone number : </label>
        <input type="text" name="number" id="number">


        <input type="submit" name="create" value="Insert"><br><br>

    
        <label for="delete">Delete row number : </label>
        <input type="number" name= "id" id="delete">
        <input type="submit" name="delete" value="Delete">
</form>
</fieldset>
<?php   

?>

</body>
</html>

Thank you.

Your code doesn't make a lot of sense in terms of the deletion process.

You seem to have two parallel processes going at once, but only one of them will do anything. You need to remove the bit about deleting by ID entirely, and then also ensure the checkboxes are output within the Delete form, not in individual, separate, unrelated forms. You can only submit one HTML form at once, and for a field to be submitted, it needs to be within that form (or in HTML5, associated with the form via an attribute).

You also need to get rid of the bit of PHP form processing which deals with the single-ID deletion process, as it's preventing the checkbox-processing code from running due to the elseif logic.

Logically it also makes sense to have the insert and deletion data in separate forms, then you only submit what's actually necessary for each process, and there are no problems with data validation etc.

Change your form code as follows:

<?php
$conn= mysqli_connect("localhost","root","" );
mysqli_select_db($conn, "phonebook_assignement");

$result = mysqli_query($conn, "SELECT * FROM contacts");
$checkboxes = "";

while ($rw=mysqli_fetch_row($result))
  $checkboxes .= '<input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $rw["id"]; ?>'."ID: ".$rw[0]."- Name: ". $rw[1]."- Phone Number: ".$rw[2]."<br>";
}
?>
<form method="POST" action="dbassignment_testcheckbox.php">
  <label for="name">Insert new name : </label>
  <input type="text" name= "name" id="name">
  <label for="number">Insert new phone number : </label>
  <input type="text" name="number" id="number">
  <input type="submit" name="create" value="Insert"><br><br>
</form>

<form method="POST" action="dbassignment_testcheckbox.php">
  <label for="delete">Delete row numbers: </label>
  <?php
  echo $checkboxes;
  ?>
  <input type="submit" name="delete" value="Delete">
</form>

Then also remove the first block of code which starts with elseif (isset($_POST["delete"])){ , and everything within it.


PS You need to urgently review the way you write SQL queries, as per my comments above, and learn to use prepared statements and parameters, in order to include user input into your queries in a secure and reliable way.

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