简体   繁体   English

使用 php 从 mysql 中删除选定的复选框行

[英]Delete selected checkbox rows from mysql using php

I am having troubles with deleting the selected checkbox rows from mysql using php code.我无法使用 php 代码从 mysql 中删除选定的复选框行。 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.一旦我 select 要删除的复选框,它会加载空白页并且没有任何内容被删除。

The first two parts with inserting new values and deleting the row by typing the ID work correctly.插入新值和通过键入 ID 删除行的前两部分工作正常。

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.您需要完全删除有关按 ID 删除的位,然后还确保复选框是删除表单的 output,而不是单独的、单独的、不相关的 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).您一次只能提交一个 HTML 表单,并且要提交的字段必须在该表单内(或在 HTML5 中,通过属性与表单关联)。

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.您还需要摆脱处理单 ID 删除过程的 PHP 表单处理位,因为它会阻止复选框处理代码由于elseif逻辑而运行。

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.从逻辑上讲,将插入和删除数据放在单独的 forms 中也是有意义的,然后您只提交每个过程实际需要的内容,并且数据验证等没有问题。

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.然后还删除以elseif (isset($_POST["delete"])){开头的第一个代码块,以及其中的所有内容。


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. PS 您需要紧急检查您编写 SQL 查询的方式,根据我上面的评论,并学习使用准备好的语句和参数,以便以安全可靠的方式将用户输入包含到您的查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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