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 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码