代码逻辑不应该允许查询通过,但它仍然允许PHP SQL


Code logic should not allow query to go through but it still does PHP SQL

我从URL收到一个$_GET['id'] -如果不转到404页面,我希望它只是数字。我相信我应该工作——但我有新的遗物告诉我不是这样,我很难办。(注意我正在使用遗留代码,所以我为mysql_query命令道歉,我知道我应该使用PDO代替)

    if(isset($_GET['item']) && is_numeric($_GET['item'])) {
        $id = $_GET['item'];
    }
    else {
        $timestamp = date("Y-m-d");
        $get_item = json_encode($_GET['item']);
        $err = date("Y-m-d").": Get Item is wrong:".$get_item."'n";
        $file = "/logs/error_log_".$timestamp.".log";
        file_put_contents($file, $err, FILE_APPEND | LOCK_EX);
        header( 'Location: http://www.example.com/404.php' ) ;
    }
    $iquery = "SELECT * FROM products WHERE products_id = $id";
    $iresult = mysql_query($iquery);
    if ($iresult == false) {
        $timestamp = date("Y-m-d");
        $err = date("Y-m-d").": SQL:".$iquery."'n";
        $file = "logs/error_log_".$timestamp.".log";
        file_put_contents($file, $err, FILE_APPEND | LOCK_EX);        
        header( 'Location: http://www.examples.com/404.php' ) ;
    }
    $iline = mysql_fetch_array($iresult, MYSQL_ASSOC);

根据我的日志,查询本身没有收到任何东西,所以$id没有收到任何东西,我的日志文件也没有收到任何东西,new relic也没有收到任何东西,告诉我查询失败了。

我不明白

似乎您希望脚本在调用header()后停止,但是代码中没有任何内容会导致这种情况发生。header()只是添加了一个HTTP头响应;它不会结束你的脚本。如果您只希望在$_GET['id']是数字时执行查询,那么您应该在第一个if块内执行查询,而不是在else块之后。