Msqli not INSERT

im trying to insert data from the form (via POST) into the MySQL database, it does not show any errors, but does not appear when I check it in phpMyAdmin.

<?php $amount = $_POST["amount"]; $unit = $_POST["unit"]; $date = date('Ymd H:i:s'); $host = "localhost"; $user = "root"; $pass = ""; $db = "finance"; $table = "silver"; $mysqli = new mysqli($host, $user, $pass, 'finance'); if(!$mysqli) { echo "<div class=\"error\">"; echo "No connection can be established"; echo "</div>"; die(); } if ($unit = "gram") $amount = $amount * 28.3495231; // create query $query = "INSERT INTO 'silver'.'finance' ( 'Transaction_Num', 'dtStamp', 'Amount' ) VALUES ( NULL, \'2013-07-03 06:18:16\', \'1\' );"; // execute query $mysqli->query($query); ?> 

I am running this on XAMPP 1.8.2 with PHP 5.4.16, Apache 2.4.4 and MySQL 5.6.11 on Windows XP.

+1
source share
2 answers

Do not use single quotation marks for table and field names. This should be the opposite.

Try the following:

  $query = "INSERT INTO `silver`.`finance` ( `Transaction_Num`, `dtStamp`, `Amount` ) VALUES ( NULL, '2013-07-03 06:18:16', '1' );"; 

OR / And if Transaction_Num is AUTO_INCREMENT , you may not need to embed it like:

 $query = "INSERT INTO silver.finance (dtStamp,Amount) VALUES ( '2013-07-03 06:18:16', 1);"; 

OR / And if you have dtStamp DEFAULT = CURRENT_TIMESTAMP , you may not need to insert this as:

 $query = "INSERT INTO silver.finance (Amount) VALUES (1);"; 
+2
source

You write it yourself, "it shows no errors." There are some things you need to keep in mind (and include in your checklist) with such broad analysis.

First of all, PHP does not always show errors. Regardless of whether it shows errors, it depends on the configuration of the error you are using. As you have not shown in your question, it is difficult to say if this is a problem or not, however, if you experience something unexpected (like you), you should first check how and where you report errors.

Explanation of everything Setting up PHP errors would be too big to answer here, a good resource (next to the manual that already documents everything in detail), we have this here on the Stackoverflow website:

Then you need to clarify if there are any and where Mysqli gives errors. Let's look at each step at which everything may turn out wrong.

Connection:

  $mysqli = new mysqli($host, $user, $pass, 'finance'); 

Creating an instance of Mysqli with the wrong credentials gives errors in PHP. However, the mysqli object will be created anyway. This means that the $mysqli variable will always be an object of type mysqli - regardless of whether the connection was successful or not.

Since every object in PHP is always true , type checking

 if (!$mysqli) { 

not enough to tell you if there was a connection problem - the way you do it, you do it wrong here. $mysqli always true, so the body of the if clause is never entered. Instead, you need to check if you were able to establish a connection to the server looking for a mysqli connection error. You can do this by checking the error number. This is 0 (an integer zero) if there was none, and a nonzero integer (plausible) if there was an error (because it is like an error number:

 if ($mysqli->connect_errno) { 

This property is documented in the PHP manual, see mysqli::$connect_errno . Various error numbers, which I cannot explain in detail, but you can find them in the Mysqli docs here: C.3. Server error codes and messages .

As another alternative to this - which also works to check if the previous connection is still active - run a test request:

 if (NULL === $mysqli->query("SELECT 1;")) { 

A simple query of type SELECT 1; returns NULL if you were unable to connect to the database server. This is due to many problems that may occur when connecting to the server: you may be using the wrong username or password ((HY000 / 1045) "Access is denied"), the wrong server ((HY000 / 1045) "Connection error",) or even using the wrong database ((HY000 / 1049) "Unknown database").

Running such a test request is easy to do and quickly reports if the connection went well. If you connect successfully, an object of type mysqli_result , which does not qualify as === NULL , so if-clause updates cover all of these failure and success scenarios.

It also shows you another great principle: most methods return what you can check to find that something went wrong.

Run an INSERT request:

The next place to check for errors is to execute an INSERT ... SQL query:

 // execute query $mysqli->query($query); 

As this code shows, you don't check for errors at all. Again, this is wrong. Instead, since your expectations do not match, you need to check here if the insert works or not. Again, this method returns a value, in the case of an INSERT ... query INSERT ... SQL is a boolean, regardless of whether the query worked:

 // execute query $result = $mysqli->query($query); if (!$result) { throw new Exception( sprintf( 'query "%s" failed: (%s/%d): %s', $query, $mysqli->sqlstate, $mysqli->errno, $mysqli->error ) ); } 

As shown in this example, the correct check of the return value is added again, and if something went wrong, an error message is given by reading the corresponding properties of your connection object $mysqli , here mysqli::$sqlstate , mysqli::$errno and mysqli::$error .

So, it’s better to double-check what happens before proceeding to the conclusion that “no error has been specified”. First of all, check whether the error should be indicated at all (and where, for example, on the screen or in the log file?). Or if you don’t even need to check the error conditions, since failure is always an option, and not everything that “doesn’t work” can be considered as a failure to extend the PHP extension or the PHP language, even when you use it.

0
source

Source: https://habr.com/ru/post/1495392/


All Articles