dqp4933 2018-06-08 08:52
浏览 17
已采纳

php 7.2阻止插入记录而不添加所有表的字段

I upgraded my php version from 7.1 to 7.2.

In my phpmyadmin I have a table like this one :

CREATE TABLE `images` (
  `image_id` int(11) NOT NULL,
  `image_name` varchar(255) CHARACTER SET utf8 NOT NULL,
  `image_title` varchar(255) CHARACTER SET utf8 NOT NULL ,
  `image_alternative` varchar(255) CHARACTER SET utf8 NOT NULL ,
  `image_folder` varchar(255) CHARACTER SET utf8 NOT NULL ,
  `image_status` int(11) NOT NULL ,
  `date` datetime DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

image_id is the only required field. The rest, all optional and can be empty. When I'm trying to insert a record :

INSERT INTO `images`(`image_name`,`image_status`) VALUES ("new image",1);

I'm getting this error

 MySQL said: Documentation #1364 - Field 'image_title' doesn't have a default value

To solve this issue I had to add in the table a predefined value for all fields or in my query to mention all fields even if they are empty.

INSERT INTO `images`(`image_name`, `image_title`, `image_alternative`, `image_folder`, `image_status`) 
VALUES ("new image,"","","",0)

I understand that php 7.x is more secure from the old versions, and they are trying to make better with time. But how can I solve this issue without the need to change all my tables or my queries. Thank you

  • 写回答

2条回答 默认 最新

  • dongpao1083 2018-06-08 10:29
    关注

    The only possible explanation I can find for this would be connected to the removal of the sql.safe_mode ini setting:

    sql.safe_mode boolean

    If turned on, database connection functions that specify default values will use those values in place of any user-supplied arguments. For details on the default values, see the documentation for the relevant connection functions.

    If that was on in your previous PHP version and somehow caused your chosen database function to set SQL strict mode to off, meaning your SQL session operated in non-strict mode, then that would explain the difference:

    For data entry into a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time:

    • If strict SQL mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows will have been
      inserted.

    • If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.

    https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html

    Either way, you should explicitly choose which SQL mode you want to operate in and not leave it up to implicit defaults. And you should prefer to run in strict mode and either make your columns nullable, or provide explicit default values for them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据