I want to insert in mysql database a string value as a datetime. I'm using myqli in php.
<?php
class DBConnection {
private $conn;
function __construct() {
}
function connect() {
include_once dirname(__FILE__).'/ConstEmployee.php';
$this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if(mysqli_connect_errno()) {
echo "Failed to connect to database".mysqli_connect_err();
}
return $this->conn;
}
}
Here is my function for inserting the value:
public function createCustomerOrder($id, $pickup_address, $delivery_address, $weight, $volume, $order_date) {
$op = $this->conn->prepare("INSERT INTO orders (ID_customer, pickup_address, delivery_address, weight, volume, order_date) VALUES (?, ?, ?, ?, ?, STR_TO_DATE(?,'%d-%m-%Y %H:%i:%s'))");
$op->bind_param("issdds", $id, $pickup_address, $delivery_address, $weight, $volume, $order_date);
if($op->execute()) {
return 1;
} else {
return 0;
}
}
My function returns 1 and the row is inserted in database but order_date column is null.
Exemple of given date value: 22-08-2020 10:10:10