Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting date in PHP prepared statements

Tags:

php

mysql

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?

like image 672
sridhar Avatar asked Dec 08 '22 00:12

sridhar


1 Answers

You should use the YYYY-MM-DD format:

$dat = date("Y-m-d");
like image 119
Wayne Whitty Avatar answered Dec 11 '22 12:12

Wayne Whitty