我目前正在开发酒店预订系统。
我特意打算删除正在处理的测试数据库中的字段。
我可以查看(SELECT)数据库的内容并将其放在一个表中,如果我愿意,我也想删除它。
但是我无法让它发挥作用。
管理员/表的代码。
// get the records from the database
if ($result = $mysqli->query("SELECT * FROM guestdetails")) {
// display records if there are records to display
if ($result->num_rows > 0) {
// display records in a table
echo "<table class = \"table\">";
// set table headers
echo "<tr>
<th>Guest ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Contact Number</th>
<th>Extra Requests</th>
<th>Gender</th>
<th>Action</th>
</tr>";
while ($row = $result->fetch_object()) {
// set up a row for each record
echo "<tr>";
echo "<td>" . $row->user_id . "</td>";
echo "<td>" . $row->firstname . "</td>";
echo "<td>" . $row->lastname . "</td>";
echo "<td>" . $row->email . "</td>";
echo "<td>" . $row->contactnumber . "</td>";
echo "<td>" . $row->requestMessage . "</td>";
echo "<td>" . $row->gender . "</td>";
// edit function, under construction
// echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='../guestdetails/deleteGuestDetails.php?id=" . $row->user_id . "'>Delete</a></td>";
echo "</tr>";
}
echo "</table>";
// if there are no records in the database, display an alert message
} else {
echo "No results to display!";
}
// show an error if there is an issue with the database query
} else {
echo "Error: " . $mysqli->error;
}
// close database connection
$mysqli->close();
这很有效。
以下是delete.php的代码
<?php
// connect to the database
include('dbConnect.php');
// confirm that the 'id' variable has been set
if (isset($_GET['user_id']) && is_numeric($_GET['user_id'])) {
// get the 'id' variable from the URL
$user_id = $_GET['user_id'];
// delete record from database
if ($stmt = $mysqli->prepare("DELETE FROM guestdetails WHERE user_id = ? LIMIT 1")) {
$stmt->bind_param("i",$user_id);
$stmt->execute();
$stmt->close();
} else {
echo "ERROR: could not prepare SQL statement.";
}
$mysqli->close();
// redirect user after delete is successful
header("Location: ../admin/mainADMINreserve.php");
// if the 'id' variable isn't set, redirect the user
} else {
header("Location: ../admin/mainADMINreserve.php");
}
?>
我认为问题在于此代码中的某处:(mainADMINreserve.php)
echo "<td><a href='../guestdetails/deleteGuestDetails.php?id=" . $row->user_id . "'>Delete</a></td>";
它没有在这里设置:(delete.php)
if (isset($_GET['user_id']) && is_numeric($_GET['user_id'])) {
这就是为什么我总是会重定向到delete.php中代码的else
部分。
我已经学习了好几个小时但我无法上班。顺便说一句,我从不同的网站获得了这段代码,例如here。它帮助我创造了很多,但我在删除中遇到了问题。什么似乎是问题?非常感谢你提前!] 如果有人好奇,这就是表中的样子。抱歉,我必须隐藏内容,但关键是我可以查看我从数据库输入的字段。问题在于代码中所述的删除按钮。
答案 0 :(得分:1)
应该是<td><a href='../guestdetails/deleteGuestDetails.php?user_id=
。参数名称和GET索引名称应匹配。