php mysql timestamp works only on update - php

i am using a timestamp type column in my mysql database, and i have enabled the option for auto update timestamp, but the time stamp works only on update command and not on insert
i am using navicat mysql client
Thanks in advance.

Try assigning the current timestamp as the default value along with enabling auto-update. That should work for you.
See: current_timestamp
Did you mean something like this..

To set the TIMESTAMP column in either table to the current timestamp at insert time, explicitly assign it that value.
INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html

Set the column default value to CURRENT_TIMESTAMP.
If you are using a server side language to insert data into the table, you could simply insert the current date into that column. In PHP, for example, that would be
<?php
$time = time();
?>

Related

Possible to get data added date in phpmyadmin using php mysql?

I created database but not added date column, data added last 1year but not show data added current date, possible to get data added current date, please help me
my database name studen
name
age
city
but not added date column, possible to get student joining date
No, it's not possible. If you need to know when a row was added, you should have a column like
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
and if you want to know when the row was modified, you can use a column like
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
If you don't have anything like this in the table, MySQL doesn't save the dates automatically.

TIMESTAMP on UPDATE doesn't work with PHP

I have a feeddate column with the info below..
type : timestamp
attributes : on update CURRENT_TIMESTAMP
default : CURRENT_TIMESTAMP
and when I used PHP to INSERT INTO the rows with this code ..
$badgefeed = "INSERT INTO d VALUES ('','".$userID."','Badge','','".$badgeNAME."','".$badgeTYPE."','".$badgelv."','','')";
$badgefeedQ = mysql_query($badgefeed);
(feeddate is on the last column that NULL)
This feeddate doesn't update and be like 0000-00-00 00:00:00
but it's gonna work when I used PHP to UPDATE something that already had in the table.
Did I do anything wrong with my feeddate structure or the INSERT code is incorrect ?
Your query should be:
$badgefeed = "INSERT INTO d VALUES ('','".$userID."','Badge','','".$badgeNAME."','".$badgeTYPE."','".$badgelv."','')";
Don't use any value for the last column as you are using the timestamp as default value in MySQL. Just omit the last (blank) value from your query. In such a way, value for the concerned column will be considered null and thus default timesamp will will be used.
Insead of '' use null.
This way, mysql manages the value itself.
If you specify any value, the server will take your value, regardless other triggers, settings.
If you don't include the column in the insert statement or leave it blank (null), it will get set by the server, according to the defaults. In this case the default being on update CURRENT_TIMESTAMP

How to automatically store current system time on record generation in MySQL?

I'm creating a database for an application. The table in question will be a log of all purchases made through the app, and one of the columns in the table will store the current date & time in DATETIME() (YYYY-MM-DD HH-MM-SS format).
I know the SQL function Now() returns the current system date and time in this format, but can I create the table so that each new purchase automatically stores the current system time? I'm trying to do something like this:
CREATE TABLE purchase_log (
//bunch of code
timestamp DATETIME() NOT NULL DEFAULT Now(),
//more code
);
So that by default each entry into purchase_log will automatically store the value returned by Now(), but I don't think this is valid SQL syntax. What would be the best way to do this?
Thanks!
CREATE TABLE purchase_log (
`addedwhen` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
I used the fieldname "addedwhen" as "timestamp" is a MySQL reserved word.
Ref: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
but I don't think this is valid SQL syntax.
How? It's absolutely valid; Did you actually tried running that? If not then see here http://sqlfiddle.com/#!9/e0b1a7/1
CREATE TABLE purchase_log (order_id int not null,
order_name varchar(10),
orderdate DATETIME NOT NULL DEFAULT Now()
);
insert into purchase_log(order_id, order_name) values(1,'laptop');
Add a default value to the field as CURRENT_TIMESTAMP.
However, while inserting data into the table, you will need to be careful.Either pass NULL to the field, or ensure proper timestamp.

Setting DATETIME to automatically go into database table

I have set my database field to DATETIME, and when a user submits the form I want to automatically insert the current date/timestamp. How do I do this?
So far I have..
INSERT INTO expenses(user_id, amount, currency, type, description, project_id, filename)
VALUES("30","55","2","6","jult","30",DATETIME,"P")
Use NOW() instead of DATETIME on your query.
Sidenote:
Also, you will face an error on query as you are passing just 7 columns, but you are inserting 8 values.
First, you need to create a column in your database to store your datetime.
Second, rather than using "DATETIME", using gmdate("Y-m-d H:i:s") to generate a GMT/UTC datetime or date() to a local time
There's no field name in your query that I can assume is a DATETIME field, but you can use NOW(), CURRENT_TIMESTAMP, or CURRENT_TIMESTAMP().
Of course this is assuming you're using MySQL.
The easiest way would be to set a default on the row, if you can:
CREATE TABLE expenses (
/* Other row definitions */
date DATETIME DEFAULT (strftime('%s', NOW()))
);
That way you can omit the row in your insertion statement entirely.
You could use CURRENT_TIMESTAMP where you have DATETIME.
In MySQL, you cannot use a volatile function such as now() as the default for most columns. The one exception is for a timestamp column. So the create table statement could look like:
create table expenses (
. . .,
CreatedAt timestamp default current_timestamp
);

storing the updated timestamp whenevr the query from that particular table is called

I want to store the time at which the table was accessed/modified. I know how to put current timestamp.
But what we must do to store the updated timestamp ?
I am not sure what exactly you are looking for but below query might be helpful.
you can create/upate your table to have "On Update Current_timestamp"
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
To update a timestamp when accessing a record you could use a stored procedure to select the records and then update the timestamp fields.
#Suresh Kamrushi has suggested a solution for updating the timestamp when the record is modified.

Categories