PHP未定义的变量错误(但它已定义?)

时间:2016-07-27 11:05:02

标签: php undefined

我不确定我的错误在哪里,但是它说Sort是未定义的,也是一个关于无效查询的错误:Erreur desyntaxeprèsde'GROUPBY Mall ORDER BY CounterDESC'àlaligne 1

以下是我的代码关于商店展示的摘录。

if(isset($_GET['formSubmit'])) 
    {
        $varStats = $_GET['formStats'];
        $errorMessage = "";

            switch($varStats)
            {

            case "ByAMEX": $sort = "AMEX = 1"; break;
            case "ByBDOTerminal": $sort = "BDOTerminal = 'YES'"; break;
            case "ByResto": $sort = "Resto = 'YES'"; break;
            case "ByCashOnly": $sort = "Cash = 'YES'"; break;
            case "ByPOSNotSeen": $sort = "POSNotSeen = 'YES'"; break;
            }

            $conn = db_connect();
            sortMall($conn, $sort);
            db_disconnect($conn);

            exit();
    }

以下是我初始化Sort函数的摘录:

function sortMall($conn, $sort)
{
$table = "test";
$column1 = "StoreName";
$column2 = "Mall";


$query1 = "Select $column2, Count($column1) AS Counter from $table where $sort GROUP BY $column2 ORDER BY Counter DESC"; 

$result = mysqli_query($conn, $query1);
    if ($result == FALSE)
    {
        echo "Invalid query: " . $conn->error;

        echo "<br/>";
        return;
    }

我实际上有另一个函数几乎使用相同的语法完成相同的功能,它的工作原理。我不知道为什么会这样。

3 个答案:

答案 0 :(得分:0)

$varStats可能不属于您定义的情况,请添加default案例

如果是这样,$ sort并未定义为您的问题

答案 1 :(得分:0)

试试这个

if(isset($_GET['formSubmit'])) 
    {
        $varStats = isset($_GET['formStats']) ? $_GET['formStats'] : '';
        $errorMessage = "";

            switch($varStats)
            {

            case "ByAMEX": $sort = "AMEX = 1"; break;
            case "ByBDOTerminal": $sort = "BDOTerminal = 'YES'"; break;
            case "ByResto": $sort = "Resto = 'YES'"; break;
            case "ByCashOnly": $sort = "Cash = 'YES'"; break;
            case "ByPOSNotSeen": $sort = "POSNotSeen = 'YES'"; break;
            default: $sort = 'yourvalue';// default case
            }

            $conn = db_connect();
            sortMall($conn, $sort);
            db_disconnect($conn);

            exit();
    }

可能是您的$sort未获得switch

的价值

答案 2 :(得分:0)

试试这个,

 if(isset($_GET['formSubmit'])) 
{
    $varStats = isset($_GET['formStats']) ? $_GET['formStats'] : '';
    $errorMessage = "";
    $sort = "1=1";   

        switch($varStats)
        {

        case "ByAMEX": $sort = "AMEX = 1"; break;
        case "ByBDOTerminal": $sort = "BDOTerminal = 'YES'"; break;
        case "ByResto": $sort = "Resto = 'YES'"; break;
        case "ByCashOnly": $sort = "Cash = 'YES'"; break;
        case "ByPOSNotSeen": $sort = "POSNotSeen = 'YES'"; break;
        default: $sort = 'yourvalue';// default case
        }

        $conn = db_connect();
        sortMall($conn, $sort);
        db_disconnect($conn);

        exit();
}