Delete Multiple Records in php

74 views Asked by At

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>
2

There are 2 answers

1
Qaisar Satti On BEST ANSWER

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>
0
Jera Balais On

move this before the the closing tag of the script

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