[英]php deleting a table row
I am writing a booking system for uni and have encountered an issue i need help with. 我正在为uni编写预订系统,并且遇到了需要帮助的问题。
<?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>
This code is for the selecting of which row in the table that you want to delete 此代码用于选择要删除表中的哪一行
<?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>';
}
?>
And this is the code to delete the row 这是删除行的代码
Any suggestions on where i am going wrong as the row won't delete 关于行出错的任何建议都不会删除
it seems like you are passing the tid as a GET variable in the query string but the method of the form is POST. 似乎您正在将tid作为GET变量传递到查询字符串中,但是表单的方法是POST。 Note that both the confirmBooking and denyBooking forms have the same problem 请注意,confirmBooking和denyBooking表单都存在相同的问题
<form action="denyBooking.php?tid=' . $row['id'] . '" method="post">
should be 应该
<form action="denyBooking.php?tid=' . $row['id'] . '" method="get">
You have a combination of problems that would cause your record to not delete in the database: 您遇到了多种问题,这些问题将导致您的记录无法在数据库中删除:
Solution: 解:
Create a hidden field and assign it's value using $row['id'] 创建一个隐藏字段并使用$ row ['id']为其赋值
<input type="hidden" name="rowId" value="' . $row['id'] . '">
And the id will be in $_POST['rowId']
该ID将在$_POST['rowId']
Other notes: 其他说明:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.