MySQL Paste in datetime = NOW () not working?

I have the following code (php, mysql, pdo):

$stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = NOW()"); $stmt->execute(array($party)); 

at startup, the side is correctly inserted, but the date is not inserted (as it should be) (system date and time during operation). I checked many times when the type of field for a date is the date and time.

Any ideas?

EDIT

To provide evidence and results:

suppose the following:

$ party = 'John';

the result returns:

 party | date ------------------------------------- John | 0000-00-00 00:00:00 

update:

When I run the following code directly in the mysql query browser, the insert works as it should:

Paste into agent_temp set party = 'John', date = NOW ();

return:

 party | date ------------------------------------- John | 2010-12-28 13:15:23 

ANSWERED

Well, who is ready to kill me? I have no idea what it caught up with, but unfortunately the problem seems to be related to an earlier version of php script from my machine, which is cached and still working with bad data. I updated, closed and emptied my browser, and now the script is working. We apologize for the fact that all the brains have melted a little.

+4
source share
4 answers

What about:

 $stmt = $db->prepare("INSERT INTO agent_temp SET party=?, date = ?"); $stmt->execute(array($_POST['party'], date("Ymd H:i:s"))); 
+9
source

This is a pure INSERT ; it does not update rows.

+1
source

Unconfirmed, but perhaps the problem is that “date” is a reserved word, you can try renaming your column and see if it works.

Another approach is to add a timestamp field with "ON UPDATE CURRENT_TIMESTAMP".

+1
source

I'm not sure which system you are using, but I would have thought your preparation statement would add a quote around the NOW() , as a result of which the operator will try to insert NOW () instead of running the mysql NOW() function - this way, since the field cannot store the NOW () characters, you get 000-00 .....

Out of interest, try changing the field type from DATETIME to TEXT and see what you get when you execute the command.

+1
source

Source: https://habr.com/ru/post/1333598/


All Articles