Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO Exception "SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens"

Tags:

php

mysql

pdo

I'm not familiar with PDO extension and can't find what's wrong in my code. Code snippet:

                try {
                $this->PDO->exec('SET AUTOCOMMIT = 0');
                $this->PDO->exec('START TRANSACTION');

                $this->PDO->prepare("UPDATE office_users
                                     SET balance = balance - ?
                                     WHERE id = ?")
                          ->execute(array($sbs_price, $this->user->id)
                );
                $user_balance -= $sbs_price;

                $this->PDO->prepare("UPDATE office_company
                                     SET pay_days = pay_days + ?
                                     WHERE inn = ?
                                     AND user_id = ?")
                          ->execute(array(
                                $sbs_period_days,
                                $company_inn,
                                $this->user->id)
                );

                $fin_string = $company_name.' ИНН '.$company_inn.' продление '.$sbs_period_month. ' мес.';

                $this->PDO->prepare("INSERT INTO office_fin_transactions
                                     (user_id, date_register, dsc, amount, status)
                                     VALUES (?, ?, ?, ?, ?)")
                          ->execute(array(
                                  $this->user->id.
                                  date("Y-m-d H:i:s"),
                                  $fin_string,
                                  $sbs_price,
                                  0)
                          );


                $this->PDO->exec("COMMIT");

                } catch (PDOException $Exception) {
                    $this->PDO->exec("ROLLBACK");
                    echo json_encode(array('result' => false,
                                            'error' => $Exception->getMessage()));
                    exit;
                }

            echo json_encode(array('result'=>'success',
                                   'inn' => $company_inn,
                                   'sbs_period' => $sbs_period_month,
                                   'company_name' => $company_name,
                                   'balance' => $user_balance)
            );
            exit;

Before this code executed, at the start of the script, PDO is configured this way:

        $this->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $this->PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);

All previous queries doesn't generate PDO exception, but last INSERT query does.

Values of variables:

$this->user->id == 158;
$fin_string == "ООО "Тестовые системы" - 2954 ИНН 123456 продление 6 мес."
$sbs_price == 1000;

Stucture of the table office_fin_transactions is:

enter image description here

What's wrong? Please help, if you have any ideas.

like image 925
d7r Avatar asked Sep 20 '25 01:09

d7r


1 Answers

You have a dot instead of a comma after $this->user->id, so it's actually only a 4-element array.

like image 106
Isvara Avatar answered Sep 21 '25 13:09

Isvara