Home  >  Article  >  Backend Development  >  TP报number of bound variables does not match number of tokens

TP报number of bound variables does not match number of tokens

WBOY
WBOYOriginal
2016-10-11 14:23:111576browse

  • PHP version: PHP 5.4.16

  • ThinkPHP version: 3.2.3

Go directly to the code:

<code>$data:

Array
(
    [area] => aa
    [brand] => bb
    [color] => cc
    [type] => 通勤车
    [alerted_police] => 0
    [status] => 0
    [lost_time] => 
    [info] => dd
    [email] => email@email.com
    [update_time] => 
    [timestamp] => 1
    [img_info_id] => 201610081621501801716297
)</code>

$sql = $ReportLost->fetchSql(true)->add($data);

<code>INSERT INTO `report_lost` (`area`,`brand`,`color`,`type`,`alerted_police`,`status`,`lost_time`,`info`,`email`,`update_time`) VALUES ('aa','bb','cc','通勤车','0','0',NULL,'dd','email@email.com',NULL)</code>

Next execute $result = $ReportLost->add($data); and an error will be reported:

<code>ERR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens</code>

However, the in $sql

is executed directly in mysql
<code>mysql> INSERT INTO `report_lost` (`area`,`brand`,`color`,`type`,`alerted_police`,`status`,`lost_time`,`info`,`email`,`update_time`) VALUES ('aa','bb','cc','通勤车','0','0',NULL,'dd','email@email.com',NULL);
Query OK, 1 row affected (0.02 sec)</code>

can be inserted.

Data table structure

<code>-- 表的结构 `report_lost`
--

CREATE TABLE IF NOT EXISTS `report_lost` (
  `id` int(11) NOT NULL,
  `title` varchar(80) CHARACTER SET utf8mb4 DEFAULT NULL,
  `keyword` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL,
  `area` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL,
  `brand` varchar(20) CHARACTER SET utf8mb4 DEFAULT NULL,
  `sub_brand` varchar(20) CHARACTER SET utf8mb4 DEFAULT NULL,
  `color` varchar(20) CHARACTER SET utf8mb4 NOT NULL,
  `type` varchar(20) CHARACTER SET utf8mb4 NOT NULL,
  `alerted_police` int(4) NOT NULL DEFAULT '0',
  `status` int(4) NOT NULL DEFAULT '0',
  `info` text CHARACTER SET utf8mb4,
  `image` text CHARACTER SET utf8mb4,
  `user` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL,
  `email` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
  `contact` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL,
  `descrpition` text CHARACTER SET utf8mb4,
  `uuid` varchar(36) CHARACTER SET utf8mb4 DEFAULT '00000000-0000-0000-0000-000000000000',
  `lost_time` timestamp NULL DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;</code>

There are indeed more fields in the table than in $data, but any undefined fields in $data can be NULL.

Is this a limitation of thinkphp? Or is it a limitation of php? Is there any solution? Do I need to define all the fields required by mysql in $data first?

Reply content:

  • PHP version: PHP 5.4.16

  • ThinkPHP version: 3.2.3

Go directly to the code:

<code>$data:

Array
(
    [area] => aa
    [brand] => bb
    [color] => cc
    [type] => 通勤车
    [alerted_police] => 0
    [status] => 0
    [lost_time] => 
    [info] => dd
    [email] => email@email.com
    [update_time] => 
    [timestamp] => 1
    [img_info_id] => 201610081621501801716297
)</code>

$sql = $ReportLost->fetchSql(true)->add($data);

<code>INSERT INTO `report_lost` (`area`,`brand`,`color`,`type`,`alerted_police`,`status`,`lost_time`,`info`,`email`,`update_time`) VALUES ('aa','bb','cc','通勤车','0','0',NULL,'dd','email@email.com',NULL)</code>

Next execute $result = $ReportLost->add($data); and an error will be reported:

<code>ERR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens</code>

However, the in $sql

is executed directly in mysql
<code>mysql> INSERT INTO `report_lost` (`area`,`brand`,`color`,`type`,`alerted_police`,`status`,`lost_time`,`info`,`email`,`update_time`) VALUES ('aa','bb','cc','通勤车','0','0',NULL,'dd','email@email.com',NULL);
Query OK, 1 row affected (0.02 sec)</code>

can be inserted.

Data table structure

<code>-- 表的结构 `report_lost`
--

CREATE TABLE IF NOT EXISTS `report_lost` (
  `id` int(11) NOT NULL,
  `title` varchar(80) CHARACTER SET utf8mb4 DEFAULT NULL,
  `keyword` varchar(100) CHARACTER SET utf8mb4 DEFAULT NULL,
  `area` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL,
  `brand` varchar(20) CHARACTER SET utf8mb4 DEFAULT NULL,
  `sub_brand` varchar(20) CHARACTER SET utf8mb4 DEFAULT NULL,
  `color` varchar(20) CHARACTER SET utf8mb4 NOT NULL,
  `type` varchar(20) CHARACTER SET utf8mb4 NOT NULL,
  `alerted_police` int(4) NOT NULL DEFAULT '0',
  `status` int(4) NOT NULL DEFAULT '0',
  `info` text CHARACTER SET utf8mb4,
  `image` text CHARACTER SET utf8mb4,
  `user` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL,
  `email` varchar(50) CHARACTER SET utf8mb4 NOT NULL,
  `contact` varchar(50) CHARACTER SET utf8mb4 DEFAULT NULL,
  `descrpition` text CHARACTER SET utf8mb4,
  `uuid` varchar(36) CHARACTER SET utf8mb4 DEFAULT '00000000-0000-0000-0000-000000000000',
  `lost_time` timestamp NULL DEFAULT NULL,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `update_time` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;</code>

There are indeed more fields in the table than in $data, but any undefined fields in $data can be NULL.

Is this a limitation of thinkphp? Or is it a limitation of php? Is there any solution? Do I need to define all the fields required by mysql in $data first?

This problem is very strange. Can you tell me the version of MySQL, or are there other hidden problems?

Provide more detailed runtime/log reports?

TP3.2 version of the modelsave()method already supports automatic filtering of fields, so it should not be a limitation of TP, and you do not need to define all fields before saving. At least I use the save() method directly in the project without exception.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn