Postingan lainnya
penyebab error pada query update menggunakan prepare statement ?
mohon bantuanya gan,
output saat di jalankan di browser : Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables in /home/ivan/Project/PHP/Experiment/phptest/oop/blog/classes/DB.php on line 64
code saya :
public function update_data($table,$id,$fields=[]){
//for key
$x = 1;
foreach ($fields as $key => $value) {
$set .= "{$key} = ?";
if($x < count($fields)){
$set .= ", ";
}
$x++;
}
$query = "UPDATE $table SET $set WHERE id = $id";
//for value
$y = array_values($fields);
$val = implode(',',$fields);
$result = $this->conn->prepare($query);
$result->bind_param("ssss",$val);
$result->execute();
}
nilai variable $query : UPDATE tb_blog SET title = ?, dateBlog = ?, content = ?, tag = ? WHERE id = 2
nilai variable $val : title 2 coba,2018-06-20,lallalaaallala lallalaaallala lallalaaallala lallalaaallala lallalaaallala,semester 1
kira2 kesalahanya dimana ya ? terimakasish
1 Jawaban:
Maksudnya, Saat binding parameter, Paramter yg dikasih sama agan tidak sesuai yang dibutuhkan. pada
<pre> $query = "UPDATE $table SET $set WHERE id = $id"; </pre> ada 2 parameter disitu, $set dan $id tapi di
<pre> $result->bind_param("ssss",$val); </pre>
agan cuman binding satu parameter doang. so tambahi satu lagi gan
<pre> $result->bind_param("ssss",$val1); $result->bind_param("zzzz",$val2); </pre>
semoga membantu :D. hehe