如何在php中删除孔行?

时间:2019-04-28 01:12:30

标签: php jquery html

我必须按下一个按钮以显示每个订单的信息,并按下一个按钮以在发送后删除订单。我如何使删除按钮起作用。因为“查看顺序有效,但删除顺序无效。我需要使用按钮分别删除每一行。

<?php

require_once 'dbconnection.php';

$sql = "SELECT * from Orders";
$result = mysqli_query($conn, $sql);

if ($result -> num_rows > 0){
    while ($row = $result-> fetch_assoc()){
        echo "<tr><td>". $row["name"] ."</td><td>". $row["student_id"] ."</td><td>". $row["ordr_num"]
        ."</td><td><button class='btn btn-small' id='{$row["ordr_num"]}'>View Order</button></td>
         <td><button class='btn btn-small'}'>Delete</button></td></tr>";
    }
}
else{
    echo "0 result";
}

我需要能够分别删除每一行,但是我真的不知道如何删除。我试过在if之前运行删除脚本,但是没有用。

1 个答案:

答案 0 :(得分:0)

我会假设您正在学习PHP,因为开发方式是一种不好的做法。因此,我将为您提供解决方案。

<?php

require_once 'dbconnection.php';

if(isset($_GET['idDelete'])){
  $resultDelete = mysqli_query($conn, "DELETE FROM Orders WHERE ordr_num = " . $_GET['idDelete']);
  if($resultDelete->affected_rows > 0){
    echo "Order # " . $_GET['idDelete'] . " has been deleted";
  }else{
    echo "Order # " . $_GET['idDelete'] . " couldn't be deleted.";
  }
}


$sql = "SELECT * from Orders";
$result = mysqli_query($conn, $sql);

if ($result -> num_rows > 0){
    while ($row = $result-> fetch_assoc()){
        echo "<tr>
                <td>". $row["name"] ."</td>
                <td>". $row["student_id"] ."</td>
                <td>". $row["ordr_num"]."</td>
                <td><button class='btn btn-small' id='" . $row["ordr_num"] ."'>View Order</button></td>
                <td><a href='?idDelete=".$row["ordr_num"]."' class='btn btn-small'>Delete</a></td>
             </tr>";
    }
}
else{
    echo "0 result";
}

?>