当前位置:Gxlcms > PHP教程 > TP报numberofboundvariablesdoesnotmatchnumberoftokens

TP报numberofboundvariablesdoesnotmatchnumberoftokens

时间:2021-07-01 10:21:17 帮助过:5人阅读

  • PHP版本:PHP 5.4.16

  • ThinkPHP版本:3.2.3

直接上代码:

$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
)

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

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)

接下来执行$result = $ReportLost->add($data);就会报错:

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

然而在mysql中直接执行$sql中的

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)

是可以插入的。

数据表结构

-- 表的结构 `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;

表中的字段的确比$data中的要多,但是在$data中未定义的都可以NULL。

这是thinkphp的限制吗?或是php的限制?有什么解决办法,我需要在$data中把所有mysql需要的字段都先定义了吗?

回复内容:

  • PHP版本:PHP 5.4.16

  • ThinkPHP版本:3.2.3

直接上代码:

$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
)

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

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)

接下来执行$result = $ReportLost->add($data);就会报错:

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

然而在mysql中直接执行$sql中的

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)

是可以插入的。

数据表结构

-- 表的结构 `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;

表中的字段的确比$data中的要多,但是在$data中未定义的都可以NULL。

这是thinkphp的限制吗?或是php的限制?有什么解决办法,我需要在$data中把所有mysql需要的字段都先定义了吗?

这个问题很奇怪,可否告知MySQL的版本,或者存在其他隐藏问题?

提供更详细的runtime/下的日志报告?

TP3.2版本的模型save()方法已经支持自动过滤字段了,所以不应该是TP的限制,你也无需在保存前定义全部的字段。至少我在项目中直接使用save()方法无异常的。

人气教程排行