我遇到此错误:
PDOStatement :: execute()期望参数1为数组,给定字符串 在第63行的C:\ xampp \ htdocs \ Work \ AppDarbNajah \ App \ Database.php中
ArticleController代码:
public function edit() {
$id = $_GET['ID'];
if(!empty($_POST) && (!empty($_FILES))){
$this->Article->update($id, [
'ref' => $_POST['ref'],
'desig' => $_POST['desig'],
'category_id' => $_POST['category_id'],
'tva' => $_POST['tva'],
'unit_id' => $_POST['unit_id'],
'Supplier_id' => $_POST['box-infos-id'],
'thumb' => $_FILES['thumb']['name'],
'created_by' => 2855,
'updated_by' => 2855
]);
}
$categories = $this->Category->extract('ID', 'category');
$units = $this->Unit->extract('ID', 'unit');
$tva = $this->Tva->extract('ID', 'tva (%)');
$article = $this->Article->find($id);
var_dump($article);
$form = new BootstrapForm($article);
$this->render('articles/edit',compact('form', 'categories', 'units', 'tva'));
}
第63行是:
'ref' => $_POST['ref']
我没有编写所有代码,而只编写了有错误的函数
更新功能代码:
public function update($id, $fields){
$sql_pairs = [];
$attributes = [];
foreach ($fields as $k =>$v){
$sql_pairs[] = "$k = ?";
$attributes[] = $v;
}
$attributes = [];
$sql_parts = implode(', ', $sql_pairs);
$this->query("UPDATE INTO {$this->table} SET $sql_parts WHERE ID = ?", $attributes);
}
答案 0 :(得分:0)
问题是我应该在查找函数中使用数组[$ id]如下:
public function find($id){
return $this->query("SELECT * FROM {$this->table} WHERE ID = ?", [$id], true);
}
答案 1 :(得分:0)
我不确定您用于创建模型的库,但是您的功能很奇怪。
public function update($id, $fields){
$sql_pairs = [];
$attributes = [];
// this bring an idea that you create list of fields to update #ref1
foreach ($fields as $k =>$v){
$sql_pairs[] = "$k = ?";
// here you add new value for next field to attributes #ref2
$attributes[] = $v;
}
// here you reset all values collected (#ref2) #ref3
// $attributes = []; //so let remove it
// but instead we need to add ID value to our query
$attributes[] = $id;
$sql_parts = implode(', ', $sql_pairs);
// UPDATE for MySQL should be something like :
// UPDATE my_table SET col1="newval1", col2="newval2" WHERE ID = 1
// lets try to get that format
$this->query("UPDATE {$this->table} SET $sql_parts WHERE ID = ?", $attributes);
}
希望现在它将更好。...