删除php

时间:2015-06-09 07:43:49

标签: php html forms mysqli

有人能告诉我下面的代码有什么问题吗?当我单击删除按钮时,它给出了错误

  

未定义索引复选框

即使checkbox是下面input标记的名称。

我知道我还没有写过删除查询,但那不是重点。我想回复$del_id,但我一直收到错误。

<?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 个答案:

答案 0 :(得分:0)

在脚本的结束标记

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

答案 1 :(得分:0)

您的表单字段必须位于<form></form>标记

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