PHP PDO-使用bindValue更新(设置参数和其中的参数相同)

PHP PDO-使用bindValue更新(设置参数和其中的参数相同),php,pdo,Php,Pdo,这对我来说有点困惑,所以我会尽力解释清楚 我正在运行更新,但什么也没发生 这是我得到的查询: "UPDATE users SET name = :name, surname = :surname WHERE name = :name AND surname = :surname" 我这样开始查询: $data = ['name' => 'Sasha', 'surname' => 'M']; $user = $users->where(['name' => 'TestN

这对我来说有点困惑,所以我会尽力解释清楚

我正在运行更新,但什么也没发生

这是我得到的查询:

"UPDATE users SET name = :name, surname = :surname WHERE name = :name AND surname = :surname"
我这样开始查询:

$data = ['name' => 'Sasha', 'surname' => 'M'];

$user = $users->where(['name' => 'TestName', 'surname' => 'TestSurname'])->update($data);
这是更新功能:

public function update($data)
    {
        $fields = explode(',', $this->prepareFields($data));
        $values = explode(',', $this->prepareValues($data));

        $i = 0;
        $count = count($fields);

        $query = "UPDATE {$this->_tablename} SET ";

        for($i; $i < $count; $i++):
            $query .= $fields[$i] . " = " . $values[$i] . ',';
        endfor;

        $query = rtrim($query, ',');
        $query .= " WHERE " . rtrim($this->_dbWhere, ' AND ');

        $this->query($query);

        $this->bindData($data);

        $this->_dbBind = call_user_func_array('array_merge', $this->_dbBind);
        $this->bindData($this->_dbBind);

        $this->execute();

        return $this->lastInsertId();
    }
public function where($field, $value = null)
    {

        if(!is_array($field)):
            $this->_dbWhere  .= $field . ' = :' . $field . ' AND ';
            $this->_dbBind[] = [$field => $value];
        else:

           foreach($field as $key => $value):
               $this->_dbWhere .= $key . ' = :' . $key . ' AND ';
                $this->_dbBind[] = [$key => $value];
           endforeach;

        endif;

        return $this;
    }
public function bindData($data)
    {
        foreach ($data as $key => $value) :
            $this->bind(':' . $key, $value);
        endforeach;
    }
public function query($query){
        $this->stmt = $this->handler->prepare($query);
    }
    if(!is_array($field)):
        // make up a placeholder name distinct from the one used in SET clause
        $field_placeholder = ":where_".$field
        $this->_dbWhere  .= $field . ' = ' . $field_placeholder . ' AND ';
        $this->_dbBind[] = [$field_placeholder => $value];
    else:
绑定数据函数:

public function update($data)
    {
        $fields = explode(',', $this->prepareFields($data));
        $values = explode(',', $this->prepareValues($data));

        $i = 0;
        $count = count($fields);

        $query = "UPDATE {$this->_tablename} SET ";

        for($i; $i < $count; $i++):
            $query .= $fields[$i] . " = " . $values[$i] . ',';
        endfor;

        $query = rtrim($query, ',');
        $query .= " WHERE " . rtrim($this->_dbWhere, ' AND ');

        $this->query($query);

        $this->bindData($data);

        $this->_dbBind = call_user_func_array('array_merge', $this->_dbBind);
        $this->bindData($this->_dbBind);

        $this->execute();

        return $this->lastInsertId();
    }
public function where($field, $value = null)
    {

        if(!is_array($field)):
            $this->_dbWhere  .= $field . ' = :' . $field . ' AND ';
            $this->_dbBind[] = [$field => $value];
        else:

           foreach($field as $key => $value):
               $this->_dbWhere .= $key . ' = :' . $key . ' AND ';
                $this->_dbBind[] = [$key => $value];
           endforeach;

        endif;

        return $this;
    }
public function bindData($data)
    {
        foreach ($data as $key => $value) :
            $this->bind(':' . $key, $value);
        endforeach;
    }
public function query($query){
        $this->stmt = $this->handler->prepare($query);
    }
    if(!is_array($field)):
        // make up a placeholder name distinct from the one used in SET clause
        $field_placeholder = ":where_".$field
        $this->_dbWhere  .= $field . ' = ' . $field_placeholder . ' AND ';
        $this->_dbBind[] = [$field_placeholder => $value];
    else:
公共函数绑定($param,$value,$type=null){

准备字段和准备值:

public function prepareFields($data)
    {
        return $fields = implode(', ', array_keys($data));
    }

    public function prepareValues($data)
    {
        $values = implode(', :', array_keys($data));

        return ':' . $values;
    }
查询功能:

public function update($data)
    {
        $fields = explode(',', $this->prepareFields($data));
        $values = explode(',', $this->prepareValues($data));

        $i = 0;
        $count = count($fields);

        $query = "UPDATE {$this->_tablename} SET ";

        for($i; $i < $count; $i++):
            $query .= $fields[$i] . " = " . $values[$i] . ',';
        endfor;

        $query = rtrim($query, ',');
        $query .= " WHERE " . rtrim($this->_dbWhere, ' AND ');

        $this->query($query);

        $this->bindData($data);

        $this->_dbBind = call_user_func_array('array_merge', $this->_dbBind);
        $this->bindData($this->_dbBind);

        $this->execute();

        return $this->lastInsertId();
    }
public function where($field, $value = null)
    {

        if(!is_array($field)):
            $this->_dbWhere  .= $field . ' = :' . $field . ' AND ';
            $this->_dbBind[] = [$field => $value];
        else:

           foreach($field as $key => $value):
               $this->_dbWhere .= $key . ' = :' . $key . ' AND ';
                $this->_dbBind[] = [$key => $value];
           endforeach;

        endif;

        return $this;
    }
public function bindData($data)
    {
        foreach ($data as $key => $value) :
            $this->bind(':' . $key, $value);
        endforeach;
    }
public function query($query){
        $this->stmt = $this->handler->prepare($query);
    }
    if(!is_array($field)):
        // make up a placeholder name distinct from the one used in SET clause
        $field_placeholder = ":where_".$field
        $this->_dbWhere  .= $field . ' = ' . $field_placeholder . ' AND ';
        $this->_dbBind[] = [$field_placeholder => $value];
    else:

关键是在
WHERE
子句和语句的
SET
部分使用相同的占位符
:fieldname
。您确实需要纠正此处提出的其他小问题,但一个简单的解决方案是在WHERE()函数中进行此更改:

public function update($data)
    {
        $fields = explode(',', $this->prepareFields($data));
        $values = explode(',', $this->prepareValues($data));

        $i = 0;
        $count = count($fields);

        $query = "UPDATE {$this->_tablename} SET ";

        for($i; $i < $count; $i++):
            $query .= $fields[$i] . " = " . $values[$i] . ',';
        endfor;

        $query = rtrim($query, ',');
        $query .= " WHERE " . rtrim($this->_dbWhere, ' AND ');

        $this->query($query);

        $this->bindData($data);

        $this->_dbBind = call_user_func_array('array_merge', $this->_dbBind);
        $this->bindData($this->_dbBind);

        $this->execute();

        return $this->lastInsertId();
    }
public function where($field, $value = null)
    {

        if(!is_array($field)):
            $this->_dbWhere  .= $field . ' = :' . $field . ' AND ';
            $this->_dbBind[] = [$field => $value];
        else:

           foreach($field as $key => $value):
               $this->_dbWhere .= $key . ' = :' . $key . ' AND ';
                $this->_dbBind[] = [$key => $value];
           endforeach;

        endif;

        return $this;
    }
public function bindData($data)
    {
        foreach ($data as $key => $value) :
            $this->bind(':' . $key, $value);
        endforeach;
    }
public function query($query){
        $this->stmt = $this->handler->prepare($query);
    }
    if(!is_array($field)):
        // make up a placeholder name distinct from the one used in SET clause
        $field_placeholder = ":where_".$field
        $this->_dbWhere  .= $field . ' = ' . $field_placeholder . ' AND ';
        $this->_dbBind[] = [$field_placeholder => $value];
    else:

:“在调用PDOStatement::execute()时,必须为要传递到语句中的每个值包含一个唯一的参数标记。除非启用了仿真模式,否则在准备好的语句中不能多次使用同名的命名参数标记。”这意味着您不能重复使用
:name,:name
,即使它们具有相同的值。您需要新的占位符名称。请使用
$this->prepare($query);
而不是
$this->query($query);
@Mihai很抱歉,我忘记插入查询函数。查询函数正在调用prepare。您说什么都没有发生,这意味着您没有看到错误。请确保您将PDO对象设置为引发异常。默认情况下,它会自动出错。
$PDO_obj->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
我猜这应该可以,但我得到了一个错误:异常“PDOException”,在/var/www/html/PMC_Dev/app/Database.php:61中有消息“SQLSTATE[HY093]:无效参数号:parameter not defined”,第61行是这个公共函数execute(){return$this->stmt->execute();}