Im stuck on this one, recently switched to PDO to learn myself.
/**
* update
* @param string $table A name of a table to update into
* @param string $data An associative array.
* @param string $where WHERE = ?.
*/
public function update($table, $dataArr, $where)
{
$fieldDetails = NULL;
foreach( $dataArr as $key => $value)
{
$fieldDetails .= "`$key` =:$value, ";
}
$fieldDetails = rtrim($fieldDetails,', ');
echo "UPDATE $table SET ($fieldDetails) WHERE (`id`=:$where)";
$stmt = $this->prepare("UPDATE $table SET ($fieldDetails) WHERE (`id`=:$where)");
foreach($dataArr as $key => $value)
{
//Binder key till värde.
$stmt->bindValue(":$key", $value);
}
$stmt->bindValue(":$where", $where);
$stmt->execute();
}
My insert function works like a charm, but this update function won't work. I think it has to do with the id no being bind. I have searched in documentation and threads but can't find an solution.
My function call.
public function update()
{
$this->db->update(
'testtable',
array(
'text' => 'exempel',
'name' => 'exempel',
), 0);
}
Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in
How do I correctly bind the integer value I pass in with the function, to so that the statement can be executed ?.