简体   繁体   English

使用PDO更新PHP

[英]Php Update using PDO

I'm trying to build a custom class to manage operations with database. 我试图建立一个自定义类来管理数据库的操作。 I'm a beginner with OOP so if you have any suggests please tell me. 我是OOP的初学者,所以如果您有任何建议请告诉我。 By the way, i have an update method which took as parameter the name of the table, the fields to update, the values with which i want to update the fields and the fields and values to put in where clause of query. 顺便说一下,我有一个更新方法,该方法将表的名称,要更新的字段,要用来更新字段的值以及要放在查询的where子句中的字段和值作为参数。 At this time, i have two distinct arrays, one for the set part and one for the where part. 目前,我有两个不同的数组,一个用于设置部分,一个用于where部分。 I build the query string like so PDOStatement Object ( [queryString] => UPDATE Ordini SET Nome=':Nome', Cognome=': Cognome', Telefono=': Telefono' WHERE ID=':ID' ) 我像这样构建查询字符串PDOStatement Object ( [queryString] => UPDATE Ordini SET Nome=':Nome', Cognome=': Cognome', Telefono=': Telefono' WHERE ID=':ID' )

Now i would like to bind the params to the variables and execute the query and that's the problem. 现在,我想将参数绑定到变量并执行查询,这就是问题所在。 I try this way but the query don't update the fields. 我尝试这种方式,但查询不会更新字段。 - In $values i have the values i want to bind to variables in the SET part - In $wv i have the values i want to bind to variables in the WHERE part - In $fieldsQuery i have the placeholders for the SET part(":Nome" for example) - In $fieldsWhere i have the placeholders for the WHERE part -在$ values中,我具有要绑定到SET部分中的变量的值-在$ wv中,我具有要绑定到WHERE部分中的变量的值-在$ fieldsQuery中,我具有SET部分的占位符(“ :例如“ Nome”)-在$ fieldsWhere中,我有WHERE部分的占位符

How can i bind in the right way the placeholders with the variables? 如何以正确的方式将占位符与变量绑定?

public function update($table=NULL, $fieldsQuery=NULL, $fieldsValues = NULL, $whereFields=NULL, $whereValues=NULL, $whereOperators = NULL)
{
    if($fieldsQuery != NULL)
        $fields = explode(",", $fieldsQuery);
    if($fieldsValues != NULL)
        $values = explode(",", $fieldsValues);
    if($whereFields != NULL)
        $wf = explode(",", $whereFields);
    if($whereValues != NULL)
        $wv = explode(",", $whereValues);

    $fieldsQuery = array_map(function($field) { return ":$field";}, $fields);
    $bindValuesSet = array_combine($fieldsQuery, $values);
    //return an array in which every field is => Fieldname=':Fieldname' 
    $bindSetInitial = array_combine($fields, $fieldsQuery);


    //transform every item in array from field to :field
    $fieldsWhere = array_map(function($field) { return ":$field";}, $wf);
    $bindValuesWhere = array_combine($fieldsWhere, $wv);
    $bindWhereInitial = array_combine($wf, $fieldsWhere);

    //implode an array mantaining both key and value
    $fieldsValues = implode(', ', array_map(function ($v, $k) { return sprintf("%s='%s'", $k, $v); }, $bindSetInitial, array_keys($bindSetInitial)));
    $fieldsWhere = implode(', ', array_map(function ($v, $k) { return sprintf("%s='%s'", $k, $v); }, $bindWhereInitial, array_keys($bindWhereInitial)));

    $query = $this->db->prepare('UPDATE ' . $table . ' SET ' . $fieldsValues . ' WHERE ' . $fieldsWhere);

    $query->bindParam(':Nome', $values[0]);
    $query->bindParam(':Cognome', $values[1]);
    $query->bindParam(':Telefono', $values[2]);
    $query->bindParam(':ID', $wv[0], PDO::PARAM_INT);

    $query->execute();
    print_r($query->debugDumpParams());
}

':Nome' is not a placeholder for a prepared statement. ':Nome'不是准备好的语句的占位符。 It's just a string ':Nome' 只是一个字符串':Nome'

Placeholder is :Nome (without `) and without any spaces, tabs etc. Ie : Nome is not a placeholder too. 占位符是:Nome (没有`),并且没有任何空格,制表符等。即: Nome 也不是占位符

So, you query should be: 因此,您的查询应为:

UPDATE Ordini SET Nome=:Nome, Cognome=:Cognome, Telefono=:Telefono WHERE ID=:ID

And thanks to @Fred-ii- - read error handling section of PDO 还要感谢@ Fred-ii--阅读PDO的错误处理部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM