简体   繁体   中英

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). I have even made the user root and supplied my correct root password: again, I can perform updates, but no new record inserts. 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

$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.

//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.

Open phpmyadmin and goto More Tab and select Variables submenu. Scroll down to find sql mode. Edit sql mode and remove STRICT_TRANS_TABLES Save it.

My local phpmyadmin settings were different than the settings for phpmyadmin up at my new server. ... Note that to made the configuration changes above I needed to login as root at phpmyadmin ...

It works, my environment is MacOS and 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) ""

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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