这是一段 PHP 代码
if(isset($_GET['id'])) {
//do something
} else {
redirect('index.php'); //redirect is a function
}
如果id被设置了(例如:index.php?id=12),那么操作就会执行,但是如果id没有被设置(例如:index.php?id=),就会显示错误。如何解决这个问题...?
如何确定id是一个整数且不为空,然后执行特定的操作....
编辑过的内容
感谢大家的回答,但我仍然遇到了那个错误...
if(isset($_GET['id'])) { // I implemented all these codes but still....
$user= User::find_by_id($_GET['id']);
// Database Classes Fetches user info from database
}
else {
redirect('index.php'); //redirect function
}
如果id等于1或大于1,则脚本执行完美。 ( index.php?id=1 )但如果将id设置为空,即index.php?id=,则会出现错误。
代码应该自动将用户重定向到index.php页面,而不是显示错误...
错误:数据库查询失败:您的SQL语法有误;请参阅与您的MySQL服务器版本相对应的手册,了解使用正确语法的方法 'LIMIT 1' 的位置在第1行。