Skip to content
Advertisement

Inserting date in PHP prepared statements

I am trying to insert current date (in d-m-Y) in a prepared statement into mysql table. I can’t get the code right. I am calling current date in php by

$dat = date("d-m-Y");

and then including in a prepared statement like this

$stmt = $mysqli->prepare("INSERT INTO mytable (name, date) VALUES (?, ?)");
$stmt->bind_param('ss', $name, $dat);
$reslt = $stmt->execute();

In the table, the date does not get inserted. It remains 0000-00-00. How can I rectify this?

Advertisement

Answer

You should use the YYYY-MM-DD format:

$dat = date("Y-m-d");
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement