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

來源:互聯網
上載者:User
  • 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()方法無異常的。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.