什么是可变的WHERE子句添加到这个更新功能的正确方法(What is the right way

2019-10-20 01:42发布

我做在PHP数据库类。 现在,我在测试中它的更新功能。 它返回一个语法错误或取决于如何形成where子句未知列误差。

我试过了:

'woord = \'uiteindelijk\'' and 'woord = \"uiteindelijk\"' and 
'woord = "uiteindelijk"' and more

我也尝试过不同的报价,并于去功能查询backsticks但人给了我同样的错误。

我的问题是什么是形成WHERE子句的正确方法是这样举例来说,如果有可能ofcourse。 如果不是我怎样才能解决这个问题。

database.mysqli.php的一部分

<?php

class myDB {

    private $mysqli;

    public function __construct() {
        require_once('config.php');

        $this->mysqli = new mysqli(HOST, USERNAME, PASSWORD, DB_NAME);  
        if (mysqli_connect_errno()) {
            printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
        }
    }

    public function close() {
        $this->mysqli->close(); 
    }

    private function check($input) {
        if(is_string($input)) {
            $input = trim($input);
            return $this->mysqli->real_escape_string($input);   
        }       
        elseif(is_int($input)) {
            return $input;
        }
        elseif($input === true || $input === false) {
            return $input;
        }
        else {
            die('invalid input');
        }   
    }
    public function update($table, $data, $where) {
        $table = $this->check($table);
        $where = $this->check($where);
        $result = '';
        if (is_array($data)) {
            $update = array();
            foreach ($data as $key => $val) {
                $key = $this->check($key);
                $val = $this->check($val);  
                $update[] .= $key . '=\'' . $val . '\'';
            }
            $query = 'UPDATE ' . $table . ' SET ' . implode(',', $update) . ' WHERE ' . $where;
            if($this->mysqli->query($query)) {
                $result = 'Last updated row id is: '.$this->mysqli->insert_id;
            } 
            else {
                $result = $this->mysqli->error;
            }
        }   
        return $result;
    }

test.php的

<!DOCTYPE HTML>
<html>
<head>
</head>
<body>  

<?php
require_once('database.mysqli.php');
$db = new myDB;
$data = array('woord' => 'gewoontjes', 'lengte' => 10);
$result = $db->update('galgje', $data, 'woord = \'uiteindelijk\'');
echo $result;
$db->close();
?>

</body>
</html>

Answer 1:

这个问题来自于你的检查方法escape_string。 此功能是用来逃跑的一份声明中精密零件,你不能在这样一个通用的方法适用于where子句作为一个整体。

如果你肯定知道你的投入是安全的(不包含特殊字符打破了sql语句,恶意与否),则只需删除转义。

或者,如果你认为它们可能包含特殊字符,有充分的理由或可能拖累SQL注入,那么你必须提供较多受约束的界面,让您可以建立适当的转义where子句自己。 例如 :

public function update($table, $data, $woord) {
    ...
    $where = 'woord = \'' . $this->check($woord) . '\'';
    ...
}

编辑 :我知道这听起来太多约束,但是安全是要付出代价的。 对于一些更灵活的,你可以看看准备的语句。 他们让你使用占位符,例如WHERE woord = ? AND id < ? WHERE woord = ? AND id < ? ,它可以绑定变量的东西,如:

$stmt->bind_param('si', $woord, $id); // 'si' -> 1:string, 2:integer

在这种情况下,mysqli的适用上界串internaly逃逸,所以你不必担心。

请注意,您不能使用占位符替换整个where子句。 WHERE ?$stmt->bind_param('s', $where);无法正常工作。

最后一件事,PDO,替代API来访问PHP数据库,支持命名占位符( WHERE woord = :woord而不是WHERE woord = ?



文章来源: What is the right way to add a variable where-clause to this update function