Good morning. I have a problem when inserting a date in a MySQL table from PHP with a prepared query. I have the following block of code:
$query = "INSERT INTO ARTICULOS (CODIGO, FECHA) VALUES (?, ?)";
$resultado = mysqli_prepare ($conexion, $query);
if ($resultado == false)
{
echo "Error fatal";
mysqli_close($conexion);
exit();
}
$codigo = "5555";
// Cadena con la fecha
$cadena_fecha_mysql = "2015-08-24";
$objeto_DateTime = date_create_from_format('Y-m-d', $cadena_fecha_mysql);
$ok = mysqli_stmt_bind_param ($resultado, "ss", $codigo,
$objeto_DateTime);
if ($ok == true)
{
echo "Estoy en 1<br>";
$ok = mysqli_stmt_execute($resultado);
if ($ok == true)
{
echo "Estoy en 2<br>";
}
else
{
$error = mysqli_errno($conexion);
echo "Error $error en la instruccion $query<br>";
echo "2 ha fallado<br>";
}
}
else
{
echo "1 ha fallado<br>";
}
mysqli_stmt_close($resultado);
mysqli_close($conexion);
It is printed: I'm in 1
And there is an exception in the mysqli_stmt_execute line:
Catchable fatal error: Object of class DateTime could not be converted to string
I have already tried many things and nothing works for me.
The table field ARTICULOS
is type DATE
(neither DATETIME
or TIMESTAMP
)