PDO插入不与OOP一起使用

时间:2015-11-01 14:26:59

标签: php mysql pdo insert sql-insert

如果我尝试在我的数据库中插入一行,我不会收到任何错误或错误,只是根本不能正常工作......

这是我的代码:

插入行:

$inserted = $user->create(array(
    'fb_id' => $userId,
    'name' => $userData->getName(),
    'mail' => Input::get('first-time-email'),
    'password' => Hash::make(Input::get('first-time-pattern')),
    'img' => $userPicture,
    'last_ip' => $_SERVER['REMOTE_ADDR'],
    'code' => $salt
));

用户类:

public function create($fields = array()) {
    return $this->_db->insert('users', $fields);
}

数据库类:

public function insert($table, $fields = array()) {
        $keys   = array_keys($fields);
        $values = null;
        $x      = 1;

        foreach($fields as $value) {
            $values .= "?";
            if($x < count($fields)) {
                $values .= ', ';
            }
            $x++;
        }

        $sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
        if(!$this->query($sql, $fields)->error()) {
            return true;
        }

        return false;
    }

DB类的查询部分:

    public function query($sql, $params = array()) {

    $this->_error = false;

    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();
        } else {
            $this->_error = true;
        }
    }

    return $this;
}

感谢您的帮助。

0 个答案:

没有答案