Adding a column in MySQL

I write a chat using php, mysql, ajax and jQuery. I want to add a column in mysql to add the date of each message, so that with the help of ajax I can display new messages on the screen. The name of my table in mysql messages, it has 3 columns id, message and date.

$message = $_POST['messages'];
    $format="d.m.Y G:i:s";
    $DB->Query("INSERT INTO messages(message, date) VALUES('$message', 'date($format, $timestamp)' )");

So I add message and date to the table, but something is wrong, it adds
0000-00-00 00:00:00 in the date column, please help.

Answer 1, authority 100%

Alternatively, you can set up a column in the database like this:


Then you can not think about this column at all and insert something like this:

$DB->Query("INSERT INTO messages(message) VALUES('$message')");

Answer 2, authority 50%

Have you tried just NOW()instead of date($format, $timestamp)?


Do you do that?

$DB->Query("INSERT INTO messages(message, date) VALUES ('$message', NOW())");

P.S in the database you can not enter the date in your format. It is always written there as YYYY-MM-DD, you can output from it only in your own format, and the date is stored there only in this way.

Answer 3

Your date format is incorrect.

For mysql, the correct date format is:

$format="Y-m-d H:i:s";

Try this: (Changed)

$date = date("Y-m-d H:i:s");
$DB->Query("INSERT INTO messages(message, date) VALUES('$message', '$date' )");