Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically updating date column on edit in phpMyAdmin

I have a small website I'm working on and for this project I want to use phpMyAdmin's user interface to edit items directly in the database instead of building an admin user interface for the website. I'm going to quick edit items like this:

a busy cat
(source: forumbilder.se)

(Not actually using wordpress, just took a random database table to use as an example)

The problem is that I have a column holding the datetime for when the row was last edited, and I don't know how to make said column update itself automatically after an edit like this is made. Any help would be appreciated.

like image 717
Just some guy Avatar asked Sep 28 '15 09:09

Just some guy


People also ask

How do I automatically insert the date in PHPMyAdmin?

To add a date information in PHPMyAdmin you need to log in the PHPMyAdmin interface and then to enter the 'Insert' tab. Then you can choose the appropriate Insert functions for each field using the drop-down list in the functions column.

How do I get the date to automatically update in MySQL?

Learn MySQL from scratch for Data Science and Analytics You can use now() with default auto fill and current date and time for this. Later, you can extract the date part using date() function. Let us set the default value with some date.

How do I create an attribute auto increment in PHPMyAdmin?

In phpMyAdmin, navigate to the table in question and click the "Operations" tab. On the left under Table Options you will be allowed to set the current AUTO_INCREMENT value. Save this answer.


1 Answers

You can implement this by using a TIMESTAMP column appplying the ON UPDATE CURRENT_TIMESTAMP clause - See https://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html

So if you are creating the table from scratch:

CREATE TABLE t1 (
  ...
  post_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  post_content ...
  ...
);

or if you already have the table, you can alter the relevant column:

ALTER TABLE t1 MODIFY post_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

Note that the column definition in both examples sets a default value of the current date/time for new records. That is usually how this feature tends to be implemented, but of course your requirement may vary, so you may wish to set the default differently.

UPDATE: If you wish to implement this using phpMyAdmin:

  1. Browse to the table in question
  2. Click the Structure tab
  3. Click the edit icon for the relevant column
  4. In the next page, set the column properties as below:

enter image description here

like image 110
Raad Avatar answered Sep 21 '22 11:09

Raad