要求用户选择位置和菜肴以搜索附近的餐馆。现在,如果用户只选择特定的美食,所有带有美食的餐厅都应该从数据库和相同的案例位置中选择...这里只是回声第一部分而不是其他两个条件..请帮助!!请不要考虑注射 下面给出了用户重定向的php部分..
<?php
if(isset($_REQUEST['location']) && $_REQUEST['cuisine'])
{
$sql="SELECT * FROM `restaurant` WHERE `location_id`='".$_REQUEST['location']."' and `cuisine_id`='".$_REQUEST['cuisine']."' ORDER BY restaurant_name ";
echo"qu";
}
elseif(isset($_REQUEST['location']) && $_REQUEST['cuisine'] ='all cuisine')
{
$sql="SELECT * FROM `restaurant` WHERE `location_id`='".$_REQUEST['location']."' ORDER BY restaurant_name ";
echo"here are result";
}
elseif(isset($_REQUEST['cuisine']) && $_REQUEST['location'] ='all location')
{
$sql="SELECT * FROM `restaurant` WHERE `cuisine_id`='".$_REQUEST['cuisine']."' ORDER BY restaurant_name ";
echo"query";
}
else
{
echo"no result found";
}
?>
答案 0 :(得分:2)
$_REQUEST['cuisine'] ='all cuisine'
应该是
$_REQUEST['cuisine'] == 'all cuisine'
if ($a= 'a')
将返回true,它将无法比较。
如果你想比较它必须
if($a == 'a')
所以这是一个例子
$a = 'b';
if($a = 'a'){
echo 'here';
}else{
echo 'not here';
}
这将输出here
答案 1 :(得分:1)
为了使$_REQUEST['cuisine']
等于“所有菜肴”,首先需要设置变量$_REQUEST['cuisine']
。同上位置。
因此,只要isset($_REQUEST['cuisine'])
为真,$_REQUEST['cuisine'] === 'all cuisine'
就是真的(同样位置),这就是为什么这两种情况永远不会发生 - 它们都被isset($_REQUEST['location'] && $_REQUEST['cuisine'])
抢占了。
我认为您正在寻找的是:
<?php
if(isset($_REQUEST['location'], $_REQUEST['cuisine']))
{
if($_REQUEST['cuisine'] === 'all cuisine')
{
$sql="SELECT * FROM `restaurant` WHERE `location_id`='".$_REQUEST['location']."' ORDER BY restaurant_name ";
echo"here are result";
}
elseif($_REQUEST['location'] === 'all location')
{
$sql="SELECT * FROM `restaurant` WHERE `cuisine_id`='".$_REQUEST['cuisine']."' ORDER BY restaurant_name ";
echo"query";
}
else
{
$sql="SELECT * FROM `restaurant` WHERE `location_id`='".$_REQUEST['location']."' and `cuisine_id`='".$_REQUEST['cuisine']."' ORDER BY restaurant_name ";
echo"qu";
}
}
else
{
echo"no result found";
}
?>
PS:除其他评论外,http://php.net/ternary也值得注意。
答案 2 :(得分:0)
单=
分配一个值,而==
如果左侧的值等于右侧的值则返回true。
答案 3 :(得分:0)
我会这样做:
// field name in DB => field name in REQUEST
$fields = array('location_id'=>'location', 'cuisine_id'=>'cuisine');
$cond = array();
foreach($fields as $f=>$p) {
$v = isset($_REQUEST[$p]) ? mysql_real_escape_string($_REQUEST[$p]) : null;
if($v && $v!=="all $p") {
$cond[] = "`$f`='$v'";
}
}
$sql = "SELECT * FROM `restaurant` WHERE " . join(' AND ', $cond) . " ORDER BY restaurant_name";
// execute $sql query
// ...