PHP Version: PHP 5.4.16
thinkphp version: 3.2.3
Directly on the 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)
$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)
Next execution $result = $ReportLost->add($data);
will cause an error:
ERR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
In MySQL, however, the direct execution $sql
of the
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)
is available for insertion.
Data table structure
--Table structure ' report_lost '--create table IF not EXISTS ' report_lost ' (' id ' int (one) not NULL, ' title ' varchar) CHARACTER S ET utf8mb4 default null, ' keyword ' varchar (+) CHARACTER set utf8mb4 DEFAULT null, ' area ' varchar (CHARACTER set UTF) 8MB4 default NULL, ' brand ' varchar (CHARACTER) set utf8mb4 DEFAULT null, ' sub_brand ' varchar CHARACTER set UTF8MB4 DEFAULT null, ' color ' varchar (CHARACTER set UTF8MB4 not NULL, ' type ' varchar () CHARACTER SET utf8mb4 not NULL, ' Alerted_police ' int (4) NOT null default ' 0 ', ' status ' int (4) is not null default ' 0 ', ' info ' text CHARACTER SET utf8mb4, ' Image ' text CHARACTER set UTF8MB4, ' user ' varchar (CHARACTER set UTF8MB4 DEFAULT NULL, ' email ' varchar () CHARACTER Set UTF8MB4 NOT NULL, ' contact ' varchar (CHARACTER set UTF8MB4 DEFAULT NULL, ' descrpition ' text CHARACTER set UTF8MB4 , ' uuid ' varchar (CHARACTER) SET utf8mb4 DEFAULT ' 00000000-0000-0000-0000-000000000000 ', ' lost_time ' timestamp NULL DE FAULT NULL, ' Create_timE ' timestamp not null default Current_timestamp, ' update_time ' timestamp null default NULL) ENGINE=INNODB default CHARSET =UTF8MB4 Collate=utf8mb4_unicode_ci;
The fields in the table are indeed more than those in $data, but the undefined in $data can be null.
Is this the thinkphp limit? Or a PHP limit? What's the solution, do I need to define all the fields required by MySQL in $data?
Reply content:
PHP Version: PHP 5.4.16
thinkphp version: 3.2.3
Directly on the 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)
$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)
Next execution $result = $ReportLost->add($data);
will cause an error:
ERR: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
In MySQL, however, the direct execution $sql
of the
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)
is available for insertion.
Data table structure
--Table structure ' report_lost '--create table IF not EXISTS ' report_lost ' (' id ' int (one) not NULL, ' title ' varchar) CHARACTER S ET utf8mb4 default null, ' keyword ' varchar (+) CHARACTER set utf8mb4 DEFAULT null, ' area ' varchar (CHARACTER set UTF) 8MB4 default NULL, ' brand ' varchar (CHARACTER) set utf8mb4 DEFAULT null, ' sub_brand ' varchar CHARACTER set UTF8MB4 DEFAULT null, ' color ' varchar (CHARACTER set UTF8MB4 not NULL, ' type ' varchar () CHARACTER SET utf8mb4 not NULL, ' Alerted_police ' int (4) NOT null default ' 0 ', ' status ' int (4) is not null default ' 0 ', ' info ' text CHARACTER SET utf8mb4, ' Image ' text CHARACTER set UTF8MB4, ' user ' varchar (CHARACTER set UTF8MB4 DEFAULT NULL, ' email ' varchar () CHARACTER Set UTF8MB4 NOT NULL, ' contact ' varchar (CHARACTER set UTF8MB4 DEFAULT NULL, ' descrpition ' text CHARACTER set UTF8MB4 , ' uuid ' varchar (CHARACTER) SET utf8mb4 DEFAULT ' 00000000-0000-0000-0000-000000000000 ', ' lost_time ' timestamp NULL DE FAULT NULL, ' Create_timE ' timestamp not null default Current_timestamp, ' update_time ' timestamp null default NULL) ENGINE=INNODB default CHARSET =UTF8MB4 Collate=utf8mb4_unicode_ci;
The fields in the table are indeed more than those in $data, but the undefined in $data can be null.
Is this the thinkphp limit? Or a PHP limit? What's the solution, do I need to define all the fields required by MySQL in $data?
This is a strange question, can you tell the MySQL version, or are there other hidden issues?
Provide more detailed log reports under runtime/?
The TP3.2 version of the model save()
method already supports automatic filtering of fields, so it should not be a TP limit, and you do not need to define all the fields before saving. At least I use the Save () method directly in the project without exception.