从数据库中删除记录,它说请求 URL 未找到
Remove record from database, it says request URL not found
我是 PHP 的新手,我正在尝试使用 PHP 制作基本的 CRUD。我可以添加记录并将其显示在 table 中,同一行上有两个操作按钮。但是,我无法从数据库中删除记录并更新 table(删除数据)。当我将鼠标悬停在删除按钮上时,似乎正在解析变量,但是当我单击删除按钮时,它显示 URL 未找到。我在下面包含了一些代码。提前致谢。
显示数据库中所有记录的table:
<div class="form-group">
<table class='table'>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Gender</th>
<th>Location</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php
$result = $conn->query('SELECT * FROM tb_user ORDER BY id DESC') or die($conn->error);
while($row = $result->fetch_object()):?>
<tr>
<td><?php echo $row->first_name; ?> </td>
<td><?php echo $row->last_name; ?></td>
<td><?php echo $row->gender; ?></td>
<td><?php echo $row->place; ?></td>
<td colspan="2">
<a href="index.php?edit<?php echo $row->id; ?>" class="btn btn-info">Edit</a>
<a href="process.php?delete=<?php echo $row->id; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
这是“删除”代码:
if(isset($_GET['delete'])){
$uId = $_GET['delete'];
$sql = "DELETE FROM tb_user WHERE id = $uId";
$conn->query($sql);
$_SESSION['message'] = "Record has been deleted!";
$_SESSION['msg_type'] = "danger";
header("location: index.php");
}
我能够使用正确的 URL 进入空白页面,而不是收到“URL 未找到”错误。
问题是我的 process.php
文件与 index.php
文件不在同一个目录中。
刷新页面时删除的行是否消失,删除代码在index.php或process.php中,如果页面刷新后消失,则表示代码运行在index.php 并且代码不会刷新页面,如果 process.php 中的代码确保页面在同一目录中,当你说(当我点击删除按钮时它说 URL未找到)这意味着代码正在运行,因为它删除了行但没有刷新 table
- process.php 应包含在 index.php 的顶部。
如果不是(您没有将其包含在顶部),
header("Location: index.php");
将不起作用。在这种情况下,您不能使用 PHP header 函数重定向,您应该使用 javascript 重定向函数,因为 header 已经发送到缓冲区。您可以使用 ini_set
函数启用所有日志后确认。
启用所有错误和警告。
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
process.php 位于 includes/ 文件夹下。所以你可能会在重定向中遇到 404 错误。那是在删除条目后,您的 URL 必须是 includes/index.php 而不是 index.php
最简单的方法就是把process.php放在index.php
的同级目录下
-
我是 PHP 的新手,我正在尝试使用 PHP 制作基本的 CRUD。我可以添加记录并将其显示在 table 中,同一行上有两个操作按钮。但是,我无法从数据库中删除记录并更新 table(删除数据)。当我将鼠标悬停在删除按钮上时,似乎正在解析变量,但是当我单击删除按钮时,它显示 URL 未找到。我在下面包含了一些代码。提前致谢。
显示数据库中所有记录的table:
<div class="form-group">
<table class='table'>
<thead>
<tr>
<th>First name</th>
<th>Last name</th>
<th>Gender</th>
<th>Location</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php
$result = $conn->query('SELECT * FROM tb_user ORDER BY id DESC') or die($conn->error);
while($row = $result->fetch_object()):?>
<tr>
<td><?php echo $row->first_name; ?> </td>
<td><?php echo $row->last_name; ?></td>
<td><?php echo $row->gender; ?></td>
<td><?php echo $row->place; ?></td>
<td colspan="2">
<a href="index.php?edit<?php echo $row->id; ?>" class="btn btn-info">Edit</a>
<a href="process.php?delete=<?php echo $row->id; ?>" class="btn btn-danger">Delete</a>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
这是“删除”代码:
if(isset($_GET['delete'])){
$uId = $_GET['delete'];
$sql = "DELETE FROM tb_user WHERE id = $uId";
$conn->query($sql);
$_SESSION['message'] = "Record has been deleted!";
$_SESSION['msg_type'] = "danger";
header("location: index.php");
}
我能够使用正确的 URL 进入空白页面,而不是收到“URL 未找到”错误。
问题是我的 process.php
文件与 index.php
文件不在同一个目录中。
刷新页面时删除的行是否消失,删除代码在index.php或process.php中,如果页面刷新后消失,则表示代码运行在index.php 并且代码不会刷新页面,如果 process.php 中的代码确保页面在同一目录中,当你说(当我点击删除按钮时它说 URL未找到)这意味着代码正在运行,因为它删除了行但没有刷新 table
- process.php 应包含在 index.php 的顶部。
如果不是(您没有将其包含在顶部),
header("Location: index.php");
将不起作用。在这种情况下,您不能使用 PHP header 函数重定向,您应该使用 javascript 重定向函数,因为 header 已经发送到缓冲区。您可以使用ini_set
函数启用所有日志后确认。
启用所有错误和警告。
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
process.php 位于 includes/ 文件夹下。所以你可能会在重定向中遇到 404 错误。那是在删除条目后,您的 URL 必须是 includes/index.php 而不是 index.php 最简单的方法就是把process.php放在index.php
的同级目录下