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()方法無異常的。