How to create "last edited" date function in php?

Hello all,

I am developing a basic admin dashboard with the feature of
Add item
Edit item
Delete item

Delete and Edit are compete but I need a small help. I am trying to display the time when the product was “last edited”.

Here is the code of the update form:

<?php
include('./server/class/mysql_crud.php');

if(isset($_GET['id'])) {
    $db = new Database();
    $db->connect();
    // Table name, Column Names, JOIN, WHERE conditions, ORDER BY conditions
    $db->select('products', 'id, name, description, price, datePosted, lastUpdated, creatorId', NULL);
    //$db->select('products');

    $res = $db->getResult();
    //print_r($res);
    foreach ($res as $key => $mushref) {
        echo $mushref['id'];
        echo $mushref['name'];
        echo $mushref['description'];
        echo $mushref['price'];
        echo $mushref['datePosted'];
        echo $mushref['lastUpdated'];
        echo $mushref['creatorId'];
    }
} else {
    echo 'nope...';
}
?>

<form method="post" action="./server/update.php">
    <!-- Content update -->
    <input value="<?php echo $mushref['name']; ?>" name="name" />
    <input type="text" value="<?php echo $mushref['description']; ?>" name="description" />
    <input value="<?php echo $mushref['price']; ?>" name="price" />

    <!-- things users mustn't edit -->
    <input type="hidden" value="<?php echo $mushref['id']; ?>" name="id" />
    <input type="hidden" value="<?php echo $mushref['datePosted']; ?>" name="datePosted" />
    <input type="hidden" value="<?php echo $mushref['lastUpdated']; ?>" name="lastUpdated" />
    <input type="hidden" value="<?php echo date($mushref['creatorId']); ?>" name="creatorId" />
    <!-- Update button -->
    <input type="submit" name="updateProduct" id="updateProduct" value="updateProduct" />
</form>

Code for the update function:


<?php
include('class/mysql_crud.php');


if( isset($_POST['updateProduct']) ) {

    $productId    =   $_POST['id'];
    $productName  =   $_POST['name'];
    $productDesc  =   $_POST['description'];
    $productPrice =   $_POST['price'];
    $datePosted   =   $_POST['datePosted'];
    $lastUpdated  =   $_POST['lastUpdated'];
    

    $db = new Database();
    $db->connect();
    // Table name, column names and values, WHERE conditions
    $db->update('products', array(
        'name' => "$productName",
        'description' => "$productDesc",
        'price' => "$productPrice",
        'datePosted' => "$datePosted",
        'lastUpdated' => "$lastUpdated"
    ), 'id="' . $productId . '"');
    $res = $db->getResult();


    //echo count($array);
    if (count($res) == 0) {
        echo "Couldnt be Updated";
    } else {
        echo "Successfully Updated";
    }
    //print_r($res);
}
?>

Now everything is working correctly, except the “lastUpdated”.

Please advise.

Thank you!

Why would that be true?
If your pushing an update NOW… wouldnt the lastUpdate time be… NOW, and not the time you got from the database?

Also:
DANGER, WILL ROBINSON. DANGER.

You’re accepting the id as a variable from the user. A user can very easily insert someone else’s ID and alter a product they shouldnt have access to change. Be very. VERY careful with that code.

1 Like

Yes, true…but I don not understand Date and Time and how they work very well yet, that’s why I am facing issues

Then let your database handle it.

What sort of database are you connecting to? Most databases have their own ways of handling things like this. For example, in MySQL your query could say
"UPDATE products SET lastupdate = NOW() WHERE productID = 1"
and the database engine would insert its current time and date into the field for you, rather than taking an input on the field.

I am connecting to MySQL

I will try that.

Or in MySQL, you could just set the lastupdate column to automatically set itself when any other column updates:

CREATE TABLE t1 (
  ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  dt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html

(If I’ve read it correctly, that is).

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.