元刷新进入无限循环

时间:2017-02-07 05:33:24

标签: php html mysql

我编写了以下代码来删除SQL表上的条目:

echo "<td><a href='protected_page.php?action=delete&id=$id'>Borrar</a></td>";

}
if(($_GET['action'] == 'delete') && isset($_GET['id'])) { 
    $rem = "DELETE FROM busca WHERE id = '".$_GET['id']."'";
    $mysqli->query($rem);
    if($rem) {
        echo "<meta http-equiv='refresh' content='0;URL=/protected_page.php'>";            
    }
}

点击删除链接后,页面进入无限循环。

3 个答案:

答案 0 :(得分:2)

查看您的属性值:

content='0;URL='/protected_page.php''

您正在使用'分隔值,但尝试使用'个字符作为其中的数据。

这是不可能的,所以你真正说的是:

content='0;URL='

正确的语法是:

<meta http-equiv='refresh' content='0;URL=/protected_page.php'>

...没有网址部分的引号。

也就是说,元刷新是执行重定向的一种讨厌的方法。 HTTP重定向更好:

<?php header("Location: /protected_page.php"); ?>

您需要稍微调整逻辑。必须在HTTP正文之前输出HTTP标头,并且您正在进行删除&amp;在HTML输出中间重定向逻辑。

根据经验,最好将所有业务逻辑(删除内容,从数据库中获取数据等)放在PHP程序的顶部,然后完成生成HTML和其他输出的所有业务(使用您在底部的业务逻辑部分填充的变量。

答案 1 :(得分:0)

如果您真的想使用PHP,请转到:

//0 = is second after is refresh 
<?php header("Refresh: 0; URL=http://redirect-url"); ?>

如果您想使用HTML,请转到

//0 = is second after is refresh 
<meta http-equiv="refresh" content="0;http://redirect-url" />

答案 2 :(得分:-1)

使用标题代替元标记,如下所示

if($rem) { 
    header( 'Location: protected_page.php' );          
}