PHP MySQLi INSERT not working, no errors

Differs from this question , but it looks like I am not getting an error when adding information to my database.

$sql = "INSERT INTO 'nlcc_ver1'.'tUsers' ('userID', 'userName', 'userPassword', 'userHash', 'user_first_name', 'user_last_name', 'user_corps', 'is_admin', 'is_trg', 'is_sup', 'is_co') VALUES (NULL, '" . $userName . "', '" . $hash . "', '" . $salt . "', '" . $f_name . "', '" . $l_name . "', '" . $corps . "', '" . $admin . "', '" . $trg . "', '" . $sup . "', '" . $co . "')"; $hostname_Database = "localhost"; $database_Database = "nlcc_ver1"; $username_Database = "root"; $password_Database = ""; $mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $result = $mysqli_query($mysqli, $sql); echo "Query run. Inserted UserID " . mysqli_insert_id($mysqli) . "<br />"; 

Line breaks are inserted to avoid scrolling sideways ... The webpage indicates that mysqli_insert_id ($ mysqli) is 0 and nothing is added to the table in my database. I do not see an error related to the appearance of the database, and MySQL is running on my server, and phpinfo () shows how the MySQL and MySQLI extensions load. This is just a development machine, so don’t worry about security (i.e. no password). I tried to solve the problem, but I do not find too much. I do not know about object-oriented programming in PHP using β†’, I use _. Is this method supported?

+4
source share
4 answers

You mixed procedural and object oriented styles of MySQLi. This resulted in you trying to use functions like mysqli_query($mysqli) instead of member functions like $mysqli->query() . Your $mysqli is an object, not a resource descriptor.

And you do not perform error checking in your request. If you were, you would realize that you mistakenly used single quotes to separate table and field names rather than backreferences.

 $sql = "INSERT INTO `nlcc_ver1`.`tUsers` (`userID`, `userName`, `userPassword`, `userHash`, `user_first_name`, `user_last_name`, `user_corps`, `is_admin`, `is_trg`, `is_sup`, `is_co`) VALUES (NULL, '" . $userName . "', '" . $hash . "', '" . $salt . "', '" . $f_name . "', '" . $l_name . "', '" . $corps . "', '" . $admin . "', '" . $trg . "', '" . $sup . "', '" . $co . "')"; $hostname_Database = "localhost"; $database_Database = "nlcc_ver1"; $username_Database = "root"; $password_Database = ""; $mysqli = new mysqli($hostname_Database, $username_Database, $password_Database, $database_Database); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $result = $mysqli->query($sql); if (!$result) { printf("%s\n", $mysqli->error); exit(); } echo "Query run. Inserted UserID " . $mysqli->insert_id . "<br />"; 

I highly recommend using the guide as a reference. It’s clear how to use these functions when you use either the procedural or the object-oriented style of MySQLi.

+10
source
 $mysqli_query($mysqli, $sql); 

it should be

 mysqli_query($mysqli, $sql); 

OR

 $mysqli->query($sql); 

And later

 $mysqli->insert_id(); 
+4
source

Look at this:

 'nlcc_ver1'.'tUsers' 

You should use backlinks here as a quotation mark:

 `nlcc_ver1`.`tUsers` 

But however (assuming $ in $ mysqli_query is just a typo): you won’t get errors for the query unless you use mysqli_error () right after the query completes.

+2
source

SET AutoCommit = 1 before insertion

 $mysqli->query('SET AUTOCOMMIT = 1'); 
0
source

All Articles