简体   繁体   English

触发警告时,PHP未插入MySQL行

[英]PHP not inserting MySQL row when warnings are triggered

I have just inherited a PHP/MySQL application which I am providing support for. 我刚刚继承了一个我提供支持的PHP/MySQL应用程序。 I have tried to set up my local environment as close to the production environment, however somehow, the live code is managing to insert a row of data where the same code on my local setup is producing a warning and not performing the insert. 我试图将本地环境设置为与生产环境接近,但是,以某种方式,实时代码正在设法插入一行数据,而本地设置上的相同代码正在产生警告,而不执行插入操作。

I have tried turning off MySQL Strict Mode and running the query from PHP directly against the database, and I found that the warnings were generated but the insert still worked. 我尝试关闭MySQL Strict Mode并直接从PHP对数据库运行查询,但发现警告已生成,但插入仍然有效。

What is preventing the insert from happening from code within PHP in my local environment? 是什么阻止了本地环境中PHP中的代码发生插入?

UPDATE (Include Example) 更新(包括示例)

$sql="insert into table("name") values("12312341234")";     
$result=mysql_query($sql);
if ($result) {
    // do stuff
}
else {
    $error = mysql_error();
    data['error'] = $error;
}

The error message which is generated is 生成的错误消息是

"Field 'filed_name' doesn't have a default value"

So basically, what is happening on the production server post query is a new row is appearing, however locally, there is nothing. 因此,基本上,生产服务器后查询上正在发生的是新行的出现,但是在本地没有任何内容。

I will also mention, the else clause is not even present on the production code. 我还要提到,生产代码中甚至没有else子句。 I have put it there to simply find out what is going on. 我把它放在那里只是为了找出正在发生的事情。

The production enviroment may be configured to not show warnings with somethin like error_reporting(0); 生产环境可以配置为不显示警告,例如error_reporting(0); or similar configuration on php.ini. 或php.ini上的类似配置。 Maybe if you use this setup on your local enviroment the results will be similar. 也许如果您在本地环境上使用此设置,结果将是相似的。 BUT. 但。 be aware: dont see the errors and/or warnings maybe a VERY dangerous thing. 请注意:看不到错误和/或警告可能是非常危险的事情。 You should try to solve the problem that raise the warning. 您应该尝试解决引发警告的问题。

"Field 'filed_name' doesn't have a default value" “字段'filed_name'没有默认值”

Meaning of the message is that 该消息的含义是

You have a column in the table defined not null without a default value clause. 您在表中有一列定义not null没有default值子句。
And unless which I can't insert without passing a value for it through an insert statement. 除非我无法通过insert语句传递它的值,否则无法插入。

Example : 范例

create table test_table( i int not null auto_increment primary key,
                         j int not null,
                         name varchar(10) );

insert into test_table(name) values( 'Ravinder' );
ERROR 1364 (HY000): Field 'j' doesn't have a default value

Check for such field in your table and include it in your insert statement. 检查表中的此类字段,并将其包含在insert语句中。

So I have been able to replicate the behaviour. 因此,我已经能够复制行为。 I had previously tried setting sql_mode from within the database. 我以前曾尝试从数据库中设置sql_mode

SET @@global.sql_mode = '';
SET @@sql_mode = '';

This did not work. 这没有用。 I am using MySQL 64bit on Windows 7 . MySQL 64bit on Windows 7使用MySQL 64bit on Windows 7 I tried editing the my.ini file and changing 我尝试编辑my.ini文件并进行更改

sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"

to

sql-mode=

This prevented the MySQL service from starting. 这阻止了MySQL服务的启动。

In the end, I re ran the MySQLInstanceConfig.exe and unchecked the Enable Strict Mode option during the setup. 最后,我重新运行MySQLInstanceConfig.exe并在安装过程中取消选中“ Enable Strict Mode选项。

I also now think it might be possible to simply remove sql-mode= from the my.ini file all together to achieve the same result as this seems to be the only thing which is different after running the config wizard. 我现在也认为,可以将my.ini文件中的sql-mode=全部删除,以达到相同的结果,因为这似乎是运行配置向导后唯一不同的地方。

Lastly, I restarted MySQL and Apache2.2 最后,我重新启动了MySQLApache2.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM