简体   繁体   English

PHP 代码可以更新 MySQL 数据库,但不能插入

[英]PHP code can UPDATE MySQL database, but can't INSERT

My update of the database has no problems whatsoever.我的数据库更新没有任何问题。 And down in my local development environment a new record insert also has no problems.而且在我本地开发环境下插入新记录也没有问题。 I have tried changing my MySQL user many times because I thought it might be about permissions (maybe that is still the problem, I don't know).我曾多次尝试更改我的 MySQL 用户,因为我认为这可能与权限有关(也许这仍然是问题,我不知道)。 I have even made the user root and supplied my correct root password: again, I can perform updates, but no new record inserts.我什至将用户设为 root 并提供了正确的 root 密码:同样,我可以执行更新,但不能插入新记录。 Here is my code which I have shortened (ie taken out the large number of fields. It works locally, but not on the server).这是我缩短的代码(即取出大量字段。它在本地工作,但不在服务器上)。

I am on Ubuntu Linux, 16.04, PHP 7我使用的是 Ubuntu Linux、16.04、PHP 7

$message = '';

$db = new mysqli('localhost', 'root', 'notrealpword', 'mydatabase');


if ($db->connect_error){

  $message = $db->connect_error;

}else{

//    echo $message ;

}

$prep = $db->prepare("INSERT INTO users (userid, username) VALUES 
('0',?)");
//Now, you can bind some parameters.
$prep->bind_param('s',$username);

$username = "atservertest";


$prep->execute();

$numaffected = $prep->affected_rows ;

echo $numaffected;

//        echo $numaffected . " row(s) affected." ;

$prep->close();

$username must initialize first. $username必须首先初始化。

//Call this first
$username = "atservertest";
//Then use bind_param
$prep->bind_param('s',$username);

FIXED MY OWN PROBLEM:解决了我自己的问题:

This is caused by the STRICT_TRANS_TABLES SQL mode.这是由 STRICT_TRANS_TABLES SQL 模式引起的。

Open phpmyadmin and goto More Tab and select Variables submenu.打开 phpmyadmin 并转到更多选项卡并选择变量子菜单。 Scroll down to find sql mode.向下滚动以找到 sql 模式。 Edit sql mode and remove STRICT_TRANS_TABLES Save it.编辑 sql 模式并删除 STRICT_TRANS_TABLES 保存它。

My local phpmyadmin settings were different than the settings for phpmyadmin up at my new server.我的本地 phpmyadmin 设置与我的新服务器上的 phpmyadmin 设置不同。 ... Note that to made the configuration changes above I needed to login as root at phpmyadmin ... ... 请注意,要进行上述配置更改,我需要以 root 身份登录 phpmyadmin ...

It works, my environment is MacOS and PHP 7.1.12, MySQL 5.6.21它有效,我的环境是 MacOS 和 PHP 7.1.12,MySQL 5.6.21

Add code:添加代码:

$prep->execute();
// see if any error happend
var_dump($db->error_list, $db->error);

to see if any error happend.看看有没有错误发生。 If no error, then the result should be如果没有错误,那么结果应该是

array(0) {
}
string(0) ""

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

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