**
( ! ) Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in
**
Am trying to build a class to dynamically update a database but getting this error: Am struggling to understand because the parameters do actually match. Key point: On the localhost - I get the error and the code is not executed but on the live server, the code is executed although, I still get 500 internal server error. Please, what am I getting wrong? or any better idea?
1.THIS IS MY $DATA
$redirectTo = "https://helpme.com";
$data = ['loanamount' => $loanamount,
'term' => $terms,
'interest' => $interest,
'repayment' => $totalrepayment,
'monthly_repayment=?' => $monthlyrepayment,
'id' => $id];
2.THIS IS MY CLASS
public function updateMultiple($data, $dbtable, $identifier,
$identifier_ans, $redirectTo)
{try{ $implodeKey = implode('=?, ', array_keys($data));
$implodeKey = rtrim($implodeKey, ', id');
$implodeValue = implode(', ', array_values($data));
$sql = "UPDATE $dbtable SET $implodeKey WHERE $identifier =?";
$stmt = $this->connect()->prepare($sql);
$result = $stmt->execute([$implodeValue]);
if($result){$redirectTo;}}
catch (PDOException $e){echo $e->getMessage(), PHP_EOL;} }
I FIXED IT BY USING EXPLODE() TO CONVERT THE $implodeValue back to an ARRAY
public function updateMultiple(array $data, $dbtable, $identifier,
int $identifier_ans)
{$implodeKey = implode('=?, ', array_keys($data));
$implodeKey = rtrim($implodeKey, ', id');
$implodeValue = array_values($data);
$implodeValue = implode(', ', $implodeValue);
$implodeValue = explode(" " , $implodeValue);
$sql = "UPDATE $dbtable SET $implodeKey WHERE $identifier =?";
$stmt = $this->connect()->prepare($sql);
$stmt->execute($implodeValue);
3.INSTANTIATE AND IMPLEMENT THE CLASS
$insert_db = new Insert();
$insert_db->updateMultiple($data, 'helpme', 'help', $id, $redirectTo);
$sql(var_dump) = 'UPDATE showal_old SET loanamount=?, term=?, interest=?,
repayment=?, monthly_repayment=? WHERE id =?'
$implodeValue(print_r) `440000, 1, 44000, 484000, 484000, 13964`
- THEN I GET THIS ERROR ( ! ) Warning: PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs--- $result = $stmt->execute([$implodeValue]); (that is the error line)