繁体   English   中英

多个if和elseif无法正常工作

[英]Multiple if and elseif don't work properly

要求用户选择位置和菜肴以搜索附近的餐馆。 现在,如果用户只选择特定的美食,所有带有美食的餐厅都应该从数据库和相同的案例位置中选择...这里只是回声第一部分而不是其他两个条件..请帮助!! 请不要考虑注射
下面给出了用户重定向的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";
    }
?>
$_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输出

点击这里http://php.net/ternary

为了让$_REQUEST['cuisine']等于“所有美食”,变量$_REQUEST['cuisine']将首先需要设置。 同上位置。

因此,只要$_REQUEST['cuisine'] === 'all cuisine'都是真的(同样位置), isset($_REQUEST['cuisine'])将是真的,这就是为什么这两种情况永远不会发生的原因 -它们都被isset($_REQUEST['location'] && $_REQUEST['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也值得注意。

Single =指定一个值,而如果左侧的值等于右侧的值,则==返回true。

我想这样做:

// 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
// ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM