0

我对 PHP $GET 有疑问。我有一个这样的编辑链接:

<a href="edit.php?form_no=<?php echo $form_no; ?>"

单击后,它将重定向到根据表单编号显示数据的详细信息页面。

<?php
$form_no = $_GET['form_no'];
if wrong get form no
{
}
?>

现在,如果我输入错误的 url 网址,我将面临问题。如果用户输入错误的表单号,我想要做的是重定向到另一个页面。

有什么建议吗?

4

1 回答 1

0

因此,您需要有一个条件,即该表单 id 是否存在于数据库中,如果不存在,则为重定向

//Query

$id_to_edit = $_GET['id']; 
/* Don't forget to sanitize, i.e is your id only numeric, no html in the id etc */

$fetch_ids = mysqli_query($connection, "SELECT id FROM tbl_name WHERE id = $id_to_edit");

if(mysqli_num_rows($fetch_ids) == 0) { //Check, if no rows are returned so redirect
   header('Location: whatever.php');
   exit;
}

您还可以抛出 Id 不存在的错误,方法是在重定向之前设置会话,在重定向页面上抛出错误,然后取消设置会话 var

于 2013-05-09T06:43:15.303 回答