PHP从删除页面重定向

PHP从删除页面重定向,php,mysql,mysqli,Php,Mysql,Mysqli,我有两个页面:index.php和stats.php 这两个页面都包含来自数据库的相同查询,并且有一个删除按钮。按下delete按钮时,它会像这样重定向:https://example.com/delete.php?id=50;其中50是行ID delete.php如下所示: <?php include "db.php"; // Using database connection file here $id = $_GET['id']; // get id thro

我有两个页面:
index.php
stats.php

这两个页面都包含来自数据库的相同查询,并且有一个删除按钮。按下
delete按钮时,它会像这样重定向:
https://example.com/delete.php?id=50;其中50是行ID

delete.php
如下所示:

<?php

include "db.php"; // Using database connection file here

$id = $_GET['id']; // get id through query string

$del = mysqli_query($link,"DELETE FROM table WHERE id = '$id'"); // delete query

if($del)
{
    mysqli_close($link); // Close connection
    header("location:index.php"); // redirects to all records page
    exit;   
}
else
{
    echo "Error deleting record"; // display error message if not delete
}
?>
if ($del) {
   ...
   header('location: ' . $_SERVER['HTTP_REFERER'])
}

您可以使用
$\u服务器[“HTTP\u REFERER”]

它包含上一页的URL

$\u服务器['HTTP\u REFERER']
包含完整的URL。
例:
http://localhost/my-site/index.php

所以你可以这样做:

header("location:".$_SERVER['HTTP_REFERER']);

正如@Bazaim所说。使用
$\u服务器['HTTP\u REFERER']

出于您的目的,它可能是这样的:

<?php

include "db.php"; // Using database connection file here

$id = $_GET['id']; // get id through query string

$del = mysqli_query($link,"DELETE FROM table WHERE id = '$id'"); // delete query

if($del)
{
    mysqli_close($link); // Close connection
    header("location:index.php"); // redirects to all records page
    exit;   
}
else
{
    echo "Error deleting record"; // display error message if not delete
}
?>
if ($del) {
   ...
   header('location: ' . $_SERVER['HTTP_REFERER'])
}

另外:您正在msql查询中使用
$\u GET['id']
。如果我想在您的URL中添加
?id=0'或'1=1
。(id=0%27%20或%20%271=1)这可能会删除整个表。您可能想阅读一些有关mysql注入的内容。

您可以将一个参数传递到包含当前页面的删除页面,以便从删除页面获取该参数,并根据该参数中的值重定向。或者您可以使用
HTTP\u REFERER
您试图调试该问题的原因是什么?如果您认为基于
$\u SERVER['HTTP\u REFERER']
的检查不起作用,您是否调试了该变量包含的内容?您可以看看我编辑的问题,我在哪里尝试使用了您的解决方案?也许你们知道我做错了什么。我已经在我的答案中添加了更多的细节。哦,所以我根本不需要那个条件,酷!谢谢:)我尝试了您提供的SQL注入添加,但它给了我“错误删除记录”,所以我想我是安全的,或者?天哪,是的,整个表都被删除了。您建议我如何避免这种情况?您可以使用mysql\u real\u escape\u string之类的工具。但是tbh。使用类似PDO的东西。中有一篇文章可能会指导您: