0

An INSERT statement, which I am using to post data derived from an HTML form into the MySQL database, does not properly execute. The execute statement returns false. However, no error message is issued. I really don't know where the problem lies. Here's the code:

Input data from form:

$order_id = $_POST['order_id'];
$container_id = $_POST['container_id'];
$quantity = $_POST['quantity'];
$confirmation_date = $_POST['confirmation_date'];

SQL statements:

$query = "INSERT INTO confirmation (order_id, container_id, quantity, confirmation_date) VALUES (?, ?, ?, ?)";
if (!($stmt = mysqli_prepare($dbc, $query))) echo "Error 1";
if (!(mysqli_stmt_bind_param($stmt, "iiis", $order_id, $container_id, $quantity, $confirmation_date))) echo "Error 2";
if(!(mysqli_stmt_execute($stmt))) echo "Error 3";
$affected_rows = mysqli_stmt_affected_rows($stmt);
mysqli_stmt_close($stmt);
if(!$affected_rows == 1) {
    echo mysqli_error($dbc);
    mysqli_close($dbc);
}

The affected rows are always 0 and no entry to the database is made.

The MySQL table is named "confirmation". It has 5 columns:

confirmation_id (primary, auto increment),
order_id (int(5)),
container_id (int(7)),
quantity (int(10)),
confirmation_date (date)

The connection to the database works, as I already use it in other scripts.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Instead of guessing or using a generic error message during testing, you should check for [mysqli errors](http://php.net/manual/en/mysqli.error.php) to find out the exact reason. – aynber Oct 27 '20 at 12:04
  • @anyber I indeed do this, added the respective line of code in my post. But thing is that it issues no error. – sparesplice Oct 27 '20 at 12:23
  • @aynber sorry I realized just now that I need to print the specific error message after each sqli command. The date format seems to be the problem. Thank you! – sparesplice Oct 27 '20 at 12:35
  • Please don't check manually for errors. You should remove all these `if` statements and enable mysqli error reporting. Do not use `mysqli_error` as this will not tell you the error for statements. – Dharman Oct 27 '20 at 13:27

0 Answers0