简体   繁体   中英

Delete Multiple Records in php

Can anyone tell me whats the problem with the code below? When I click the delete button, it gives me an error of

undefined index checkbox

even though checkbox is the name of the input tag below.

I know I haven't written the delete query, but thats not the point. I want to echo the $del_id , but I keep getting the error.

<?php

  include 'connection.php';
  if(isset($_POST['del'])){
    $name=$_POST['checkbox'];
    $del_id=implode(",", $name);    
    echo $del_id;

  }

  $sql="SELECT * FROM `student-reg`";
  $result=mysqli_query($conn, $sql) or die(mysqli_error($conn));
  echo "<table align='center' border='2'>
          <tr>
            <th>Mark</th>
            <th>ID</th>
            <th>First_Name</th>
            <th>Last_Name</th>
            <th>Roll_no</th>
            <th>Degree</th>
          </tr>
        ";

  while($row=mysqli_fetch_assoc($result)){
    $id=$row['Id'];
    echo "
      <tr> 
        <td><input type='checkbox' name='checkbox[]' value='".$row['Id']."'></td>
        <td>{$id}</td> 
        <td>{$row['First_name']}</td> 
        <td>{$row['Last_name']}</td>
        <td>{$row['Roll_no']}</td>
        <td>{$row['Degree']}</td>
      </tr>
    ";
  }
?>

<html>
<body>

  <form method="POST">
    <input type="submit" value="Delete" name="del">
  </form>

</body>
</html>

move this before the the closing tag of the script

if(isset($_POST['del'])){
$name=$_POST['checkbox'];
$del_id=implode(",", $name);    
echo $del_id;

you form fields must be in <form></form> tag

<?php

     include 'connection.php';
    if(isset($_POST['del'])){
    $name=$_POST['checkbox'];
    $del_id=implode(",", $name);    
    echo $del_id;


     }

     $sql="SELECT * FROM `student-reg`";
    $result=mysqli_query($conn, $sql) or die(mysqli_error($conn));
    echo "<table align='center' border='2'>
    <tr>
    <th>Mark</th>
    <th>ID</th>
    <th>First_Name</th>
    <th>Last_Name</th>
    <th>Roll_no</th>
    <th>Degree</th>
    </tr>
    "; ?>
<form method="post">

<?php

    while($row=mysqli_fetch_assoc($result)){
      $id=$row['Id'];
      echo "
      <tr> 
      <td><input type='checkbox' name='checkbox[]' value='".$row['Id']."'></td>
          <td>{$id}</td> 
      <td>{$row['First_name']}</td> 
          <td>{$row['Last_name']}</td>
          <td>{$row['Roll_no']}</td>
          <td>{$row['Degree']}</td>

          </tr>
        ";

      }
     ?>

      <html>
     <body>




     <input type="submit" value="Delete" name="del">
     </form>

     </body>
     </html>

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