无法将有效变量传递给查询
我正在将变量传递到我的页面 page?title=fred
但如果它的值错误(不在数据库中),我想重定向到另一个页面,例如:fred
第一部分检查变量是否为空。这似乎有效。
//get variable from title
if(!isset($_GET['title']) || empty($_GET['title'])) {
echo "no variable";
}
else {
$get_title = mysqli_real_escape_string($db, $_GET['title']);
$title = str_replace("-"," ", $get_title);
}
我无法检查我传递的变量是否在数据库中以及是否重定向到主页。
$query = mysqli_query($db, "SELECT title FROM persons WHERE title = '$title'");
//check that variable is in the database
if(!isset($query)){
while($row = mysqli_fetch_assoc($query)){
echo $row['title'];
}
// mysqli_free_result($query);
}
else {echo "variable does not exist";} ?>
有人可以告诉我我做错了什么以及如何解决它吗?谢谢你,
I'm passing variables to my page page?title=fred
but I would like to redirect to another page if it's the wrong value(not in the database) ex: fredd
The first part checks if the variable is empty. Which seems to be working.
//get variable from title
if(!isset($_GET['title']) || empty($_GET['title'])) {
echo "no variable";
}
else {
$get_title = mysqli_real_escape_string($db, $_GET['title']);
$title = str_replace("-"," ", $get_title);
}
I'm having trouble checking if the variable I pass is in the database and if it's not to redirect to main page.
$query = mysqli_query($db, "SELECT title FROM persons WHERE title = '$title'");
//check that variable is in the database
if(!isset($query)){
while($row = mysqli_fetch_assoc($query)){
echo $row['title'];
}
// mysqli_free_result($query);
}
else {echo "variable does not exist";} ?>
Can someone please show me what I am doing wrong and how to fix it? Thank you,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$query
始终被设置(您只需设置它),因此您想要的代码永远不会执行。你想要这个:
$query
is always set (you just set it) so the code you want will never execute.You want this instead:
isset($query) 将始终为 true - mysqli_query() 将始终返回某些内容,即使它只是一个布尔假值。
isset($query) will ALWAYS be true - mysqli_query() will always returning SOMETHING, even if it's just a boolean false value.