John Davidson

mysql - how to update current timestamp when user updates record each time in php pdo

0 comments
Message:


This is the table -



In the above demo file I have stored the image name and path and an automatic timestamp when image is uploaded.


Now when I update the image , I can't get the 'date_of_entry' column to update each time I update the old image.


And the 'date_of_entry' does not get inputed manually by user in form .


So how can i make the 'date_of_entry' field to UPDATE timestamp each time when image gets updated?



My Answer:
You can achieve this by using a trigger in MySQL. Here's an example of how you can create a trigger to update the 'date_of_entry' column each time the record is updated:

1. Create a trigger in your MySQL database:

sql
CREATE TRIGGER update_date_of_entry
BEFORE UPDATE ON your_table_name
FOR EACH ROW
SET NEW.date_of_entry = CURRENT_TIMESTAMP;


Replace 'your_table_name' with the actual name of your table.

2. Update your PHP code to include the 'date_of_entry' column in the UPDATE query:

php
$stmt = $pdo->prepare("UPDATE your_table_name SET image_name = :image_name, image_path = :image_path WHERE id = :id");
$stmt->bindParam(':image_name', $image_name);
$stmt->bindParam(':image_path', $image_path);
$stmt->bindParam(':id', $id);
$stmt->execute();


With this trigger in place, the 'date_of_entry' column will automatically be updated to the current timestamp each time a record is updated in the table.

Rate this post

4 of 5 based on 7220 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us