我有一个表(php),每行都有删除按钮。我试图通过提交按钮将行的id传递给我的查询。我提交时没有错误,但没有删除该行。
这是我的表源代码:
<?php
include ("dbh.php");
$sql_showBooking = "SELECT tid, ticket, bk_email, price FROM ticket_purchase";
$result = $conn->query($sql_showBooking);
if ($result->num_rows > 0) {
echo "<table><tr><th>No.</th><th>Ticket</th><th>Email</th><th>Price</th>
<th>Action</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["tid"]."</td>";
echo "<td>".$row["ticket"]."</td>";
echo "<td>".$row["bk_email"]."</td>";
echo "<td>".$row["price"]."</td>";
echo '<form novalidate="novalidate" role="form" method="post" action="delete.php?id='.$row['tid'].'">
<td><input type="submit" class="btn btn-block" name="btnDelete" value="Delete"/></td>
</from>';
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
PHP(删除)
<?php
session_start();
include ("dbh.php");
if(isset($_POST["btnDelete"])){
$tid_delete = $_POST ['tid'];
$sql_delete = "delete from ticket_purchase where tid='$tid_delete'";
mysqli_query($conn, $sql_delete);
mysqli_close($conn);
header("Location: Coldplay.php");
}
?>
有什么想法吗?
答案 0 :(得分:0)
虽然表单确实发送了POST请求,但数据本身是通过GET发送的,如action属性所示:action="delete.php?id='.$row['tid'].'">
您可以在delete.php中使用$ _GET而不是$ _POST,或者您可以向表单添加隐藏输入并将其值设置为$ row ['tid']:
<input type = "hidden" value = "<?= $row['tid'] ?>" />
答案 1 :(得分:0)
试试这个......
<?php
include ("dbh.php");
$sql_showBooking = "SELECT tid, ticket, bk_email, price FROM ticket_purchase";
$result = $conn->query($sql_showBooking);
if ($result->num_rows > 0) {
echo "<table><tr><th>No.</th><th>Ticket</th><th>Email</th><th>Price</th>
<th>Action</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>".$row["tid"]."</td>";
echo "<td>".$row["ticket"]."</td>";
echo "<td>".$row["bk_email"]."</td>";
echo "<td>".$row["price"]."</td>";
echo '<td><a href = "delete.php?tid='.$row['tid'].' ">Delete</a></td>'; //change this line
echo "</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
<?php
session_start();
include ("dbh.php");
if(isset($_REQUEST["tid"])){ //change
$tid_delete = $_REQUEST['tid']; //changes
$sql_delete = "delete from ticket_purchase where tid='$tid_delete'";
mysqli_query($conn, $sql_delete);
mysqli_close($conn);
header("Location: Coldplay.php");
}
?>