doulu1968 2019-03-04 18:27
浏览 35

我试图构建一个类来动态更新数据库,但收到此错误:

**

( ! ) 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`
  1. 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)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 有人能看一下我宿舍管理系统的报修功能该怎么改啊?链表那里总是越界
    • ¥15 cs loadimage运行不了,easyx也下了,没有用
    • ¥15 r包runway详细安装教程
    • ¥15 Html中读取Json文件中数据并制作表格
    • ¥15 谁有RH342练习环境
    • ¥15 STM32F407 DMA中断问题
    • ¥15 uniapp连接阿里云无法发布消息和订阅
    • ¥25 麦当劳点餐系统代码纠错
    • ¥15 轮班监督委员会问题。
    • ¥20 关于变压器的具体案例分析