Php 为什么我能';t如果';id';不存在?
我试图将不存在ID的查询重定向到主页,但总是会出现由以下条目引起的错误Php 为什么我能';t如果';id';不存在?,php,Php,我试图将不存在ID的查询重定向到主页,但总是会出现由以下条目引起的错误 "<?php echo $row['script']; ?>" 我认为您的问题可能是您正在使用empty($result))而$result不应该为空,即使查询没有返回任何内容。相反,请尝试使用: $result不是空的如果成功执行,则需要从$result中获取行数,以确定是否找到结果: if (mysqli_num_result($result) > 0) { header('Location
"<?php echo $row['script']; ?>"
我认为您的问题可能是您正在使用
empty($result))
而$result
不应该为空,即使查询没有返回任何内容。相反,请尝试使用:
$result
不是空的
如果成功执行,则需要从$result中获取行数,以确定是否找到结果:
if (mysqli_num_result($result) > 0) {
header('Location: index.php');
exit;
}
您还应该验证/过滤您的输入
PHP提供了一组最小的验证/筛选函数:
在您的情况下,应该筛选INT
:
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) { // Validation fails. It is recommended you return an error to your user, but in this case I'm going to follow your logic.
$id = 1;
}
// Do your database queries from here...
$row是在“if(isset($\u GET['id']))”块中定义的,我猜只要您离开这个if块,它就会被取消设置,因为它以前没有定义过
在一个较新版本的php中,不能使用isset()检查数组索引,最好使用array\u key\u exists()
if (mysqli_num_result($result) > 0) {
header('Location: index.php');
exit;
}
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if (!$id) { // Validation fails. It is recommended you return an error to your user, but in this case I'm going to follow your logic.
$id = 1;
}
// Do your database queries from here...