The error occurs when I try to update the ‘name’ and ‘description’ in the database.
Full error:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in ...db.php:126 Stack trace: #0 ...db.php(126): PDOStatement->execute() #1 C:OSPaneldomainsteachappcontrollerstopics.php(76): update() #2 ...edit.php(3): include('...') #3 {main} thrown in ...db.php on line 126"
File: db.php
function update($table, $id, $params){
global $pdo;
$i = 0;
$str = '';
foreach ($params as $key => $value) {
if ($i === 0){
$str = $str . $key . "= '" .$value . "'";
}else {
$str = $str .", " . $key . " = '" . $value . "'";
}
$i++;
}
$sql = "UPDATE $table SET $str WHERE id =". $id;
// tt($sql);
// exit();
$query = $pdo->prepare($sql);
$query->execute($params);
dbCheckError($query);
return $pdo->lastInsertId();
}
File: topics.php
if ($name === '' || $description === ''){
$errMsg = 'Не все поля заполнены!';
}elseif(mb_strlen($name, 'UTF8') < 2){
$errMsg = "Придумайте длиннее категорию";
}else{
$topic = [
'name' => $name,
'description' => $description
];
$id = $_POST['id'];
$topic_id = update('topics', $id, $topic);
header('location: ' . BASE_URL . 'admin/topics/index.php');
}
}
File: edit.php
<?php session_start();
include('../../path.php');
include("../../app/controllers/topics.php");
?>
Unfortunately, I didn’t find any similar examples.