gpt4 book ai didi

php删除表格行

转载 作者:行者123 更新时间:2023-11-29 21:11:19 24 4
gpt4 key购买 nike

我正在为大学编写一个预订系统,遇到了一个需要帮助的问题。

<?php
$title = 'Room Booking';
require_once 'header.php';
?>
<ul>
<?php

$query = "SELECT * FROM `room1booking` ORDER BY date, start, id";

if ($result = mysqli_query($db_server, $query)) {
if (mysqli_num_rows($result) > 0) {

while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo '<li>
' . $row['name'] . '
' . $row['date'] . '
' . $row['start'] . '
<form action="confirmBooking.php?tid=' . $row['id'] . '" method="post">
<button type="submit" name="submit"><a href="confirmBooking.php?tid=' . $row['id'] . '">Confirm</a></button>
</form>
<form action="denyBooking.php?tid=' . $row['id'] . '" method="post">
<button type="submit" name="submit"><a href="denyBooking.php?tid=' . $row['id'] . '">Deny</a></button>
</form>
</li>
';

}

} else {
echo '<li>There are no results</li>';
}

mysqli_free_result($result);
}


mysqli_close($db_server);
?>
</ul>
</form>

此代码用于选择要删除表中的哪一行

<?php
$title = 'Delete The Booking';
require_once 'header.php';

if(isset($_GET['submit'])){

$tid = mysqli_real_escape_string($db_server, trim($_GET['tid']));

if(!empty($tid)){

$query = "DELETE FROM room1booking WHERE id = '$tid'";

mysqli_query($db_server, $query);

echo '<p>You have successfully deleted the booking.</p>';

}
} else {
echo '<p>There is a problem with the system.</p>';

}


?>

这是删除行的代码

关于我哪里出错的任何建议,因为该行不会删除

最佳答案

您遇到的一些问题可能会导致数据库中的记录无法删除:

  1. 按钮不会在 POST 中发送值。您需要将 id 添加到隐藏输入并在 POST 中检索值。
  2. 您还混合使用了 GET 和 POST。你的表单方法是POST,但是您正在尝试从 GET 检索变量。自从这里的操作是删除,最好使用POST(否则你需要考虑到用户能够通过更改删除任何记录url 参数)。

解决方案:

创建一个隐藏字段并使用 $row['id'] 为其分配值

<input type="hidden" name="rowId" value="' . $row['id'] . '">

ID 将位于 $_POST['rowId']

其他说明:

  1. 你应该使用isset()验证 $_POST 中是否存在“rowId”在您访问它之前
  2. 如果 rowId 应始终为整数,则您应该获取该整数使用 intval() 的字符串变量的值在使用它之前您的 SQL 查询。
  3. 你应该使用prepared statements with parameter binding在您的查询。

关于php删除表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36344372/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com