mysqli_real_escape_string删除值

时间:2018-06-02 09:47:31

标签: php database mysqli

运行用于SQL注入保护的DBEscape($ data)函数后,我得到空值。有人可以帮忙吗?

我的输入都是多个数组,例如:name =“quote [] [dt_flight]”,name =“quote [] [acft]”等等。

方法是POST。

function DBEscape($data){
    $link = DBConect();
    if(!is_array($data)){
        $data = mysqli_real_escape_string($link,$data);
    }
    else {
        $arr = $data;
        foreach ($arr as $key => $value){
            $key = mysqli_real_escape_string($link, $key);
            $value = mysqli_real_escape_string($link, $value);
            $data[$key] = $value;
        }
    }
    DBClose($link);
    return $data;
}



    function DBCreate($table, array $data, $insertId = false){
        $table = DB_PREFIX.'_'.$table;
        $data = DBEscape($data);
        var_dump($data);
        $fields = implode(", ", array_keys($data));
        $values = "'".implode("', '", $data)."'";
        $query = "INSERT INTO {$table} ({$fields}) VALUES ({$values});";
        var_dump($query);
        return DBExecute($query, $insertId);
    }


if(isset($_POST["quote"]) && is_array($_POST["quote"])){
    foreach($_POST["quote"]["dt_flight"] as $key => $text_field){
        
        $last_id = DBCreate('quote',$_POST['quote'],true);
       
       $i++;
    }  
}

连接有效,因为它将行插入表中。我在DBEscape之前和之后使用vardump来确定它正在删除值,键很好。

PS:建议的答案是单个变量而不是数组。

1 个答案:

答案 0 :(得分:0)

正如您在var_dump - 结果中看到的那样,您发送到DBCreate并因此发送到DBEscape的数据看起来像

array(
    'dt_flight' => array(0 => '2018-06-13'),
    'acft'      => array(0 => 'VQ-BFD',
    // and so on
)

您发送到

的数据
// $value = array(0 => '2018-06-13') here
$value = mysqli_real_escape_string($link, $value);

好吧,mysqli_real_escape_string不太喜欢数组,因此会返回NULL,从而在表格中插入空数据。

您很可能希望在foreach($_POST["quote"]["dt_flight"])循环中解决此错误,因为我认为您发送了多个航班数据:

foreach($_POST["quote"]["dt_flight"] as $key => $text_field) {
    // $key would be 0, for $_POST["quote"]["dt_flight"][0] = '2018-06-13'

    $keyData = [];
    foreach($_POST["quote"] as $field => $allFieldValues) {
        // Walk over every field, and add the value for the same $key
        if (is_array($data) && isset($allFieldValues[$key])) {
            // Would add for example $keyData['acft'] = $_POST['quote']['acft'][0] = 'VQ-BFD';
            $keyData[$field] = $allFieldValues[$key];
        }
    }

    var_dump($keyData); 
    // Would look like array(
    //    'dt-flight' => '2018-06-13',
    //    'acft'      => 'VQ-BFD',
    //    and so on
    // )

    $last_id = DBCreate('quote',$keyData,true);

    $i++;
}

虽然这不是你问题的一部分,但我真的建议你也关注我对mysqli_real_escape_string不是一种安全的方法来逃避列名(或表名等)的问题的评论。例如,使用以下解决方案:

function DBCreate($table, array $data, $insertId = false) {
    // For each table the known columns
    $columns = array( 'quote' => array('dt_flight', 'acft', '...') );

    // Verify valid table given
    if (!isset($columns[$table])) {
        throw new InvalidArgumentException('No such table: ' . $table);
    }

    // Remove everything from data where the key is not in $columns[$table] 
    // = Remove everything where the column-name is non-existing or even an attempt to hack your system
    $data = array_intersect_key($data, array_fill_keys($columns[$table], null));

    if (!count($data)) {
        throw new InvalidArgumentException('No (valid) data given at all');
    }

    // Next, continue with your implementation
}