I previously asked a similar question, but still didn’t get a working answer.
So I have in my database a table wich contains informations about users(first_name,email…) and 2 fields containing the date of creation(“created”) and the date of update(“updated”).
the “updated” field has the type TIMESTAMP ,default =”CURRENT_TIMESTAMP”, attributs =”on Current_timestamp” . And the “created” field has type=”datetime” and not null default=”none”.
The problem is when displaying the date of creation, I got next to each user “0000-00-00 00:00:00”.
Here is the code that adds a user to the database:
$first_name = trim($_REQUEST['first_name']);
$last_name = trim($_REQUEST['last_name']);
$username = trim($_REQUEST['username']);
$password = trim($_REQUEST['password']);
$email = trim($_REQUEST['email']);
$created = strftime("%Y-%m-%d %H:%M:%S", time());
$insert_sql = sprintf("INSERT INTO users (first_name, last_name, username, password, email, created)" .
"VALUES('%s', '%s', '%s', '%s', '%s', '%s');",
mysql_real_escape_string($first_name),
mysql_real_escape_string($last_name),
mysql_real_escape_string($username),
mysql_real_escape_string(crypt($password, $username)),
mysql_real_escape_string($email),
mysql_real_escape_string($created));
mysql_query($insert_sql) or die(mysql_error());
Please help, I tried various functions like date(), now() but still doesn’t work.
EDIT: that what my table shows:
mysql> SELECT updated, created from users;
+---------------------+---------------------+
| updated | created |
+---------------------+---------------------+
| 2012-09-21 09:09:49 | 0000-00-00 00:00:00 |
| 2012-09-21 09:27:27 | 0000-00-00 00:00:00 |
| 2012-09-22 11:36:19 | 0000-00-00 00:00:00 |
| 2012-09-22 11:43:35 | 0000-00-00 00:00:00 |
+---------------------+---------------------+
You are entering a string in a timestamp field; instead use the mysql function
NOW(). Your query should be:Also, consider migrating to
mysqlito take advantage of prepared statements.To clarify the comments; it won’t matter what type the field is, as MySQL will know how to deal with it if you give it
NOW():It won’t work, if you try to insert
UNIX_TIMESTAMP():