Mysqli绑定param更新

时间:2015-12-26 16:09:02

标签: php mysql mysqli

我刚刚开始创建mysqli bind_param更新功能。我的插入函数工作正常,但是我得到错误 - 警告:mysqli_stmt :: bind_param():类型定义字符串中的元素数与绑定变量数不匹配。我不明白问题在哪里。

My array = Array ( [bday_month] => 9 [bday_day] => 7 [bday_year] => 2003 [id] => 2 )
    public function update($table, $data) {
            if (empty($table) || empty($data)) {
                return false;
            }

            $array_slice = array_slice($data, 0, count($data)-1);

            $fields = implode(' = ?, ', array_keys($array_slice)) . ' = ?';

            $stmt = $this->db->prepare("UPDATE `{$table}` SET {$fields} WHERE `id` = ?");
            call_user_func_array(array($stmt, 'bind_param'), $this->refValues($data));
            $stmt->execute();
        }

2 个答案:

答案 0 :(得分:1)

解决。我忘记了插入类型。

答案 1 :(得分:-1)

我有一个为我工作你可以看到它

 /**
 * update
 * @author Alaa M. Jaddou
 * @param string $table A name of table to insert into
 * @param string $data An associative array
 * @param string $where the WHERE query part
 */
public function update($table, $data, $where)
{
    ksort($data);

    $fieldDetails = NULL;
    foreach($data as $key=> $value) {
        $fieldDetails .= "`$key`= ?,";
    }
    $fieldDetails = rtrim($fieldDetails, ',');

    $sth = $this->prepare("UPDATE $table SET $fieldDetails WHERE $where");

    $values = array(); 
    foreach ($data as $key => $value) {
        $values = implode(', ', $value);
    }
    $sth->bind_param($values);
    return $sth->execute();
}