PHP版本:PHP 5.4.16
ThinkPHP版本:3.2.3
直接上碼:
<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>
接下來執行$result = $ReportLost->add($data);
就會報錯:
<code>ERR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens</code>
然而在mysql中直接執行$sql
中的
<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>
是可以插入的。
資料表結構
<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>
表中的欄位的確比$data中的多,但是在$data中未定義的都可以NULL。
這是thinkphp的限制嗎?或是php的限制?有什麼解決辦法,我需要在$data中把所有mysql需要的欄位都先定義了嗎?