famak te
famak te

Reputation: 25

Automatically update date column in database

hello I have a page that accepts responses based on if a transaction was successfull or not.

So I am trying to update my database record on a successfull transaction status to paid and also update the start date to the present date and also the expiry date to a one year later date, automatically.

Presently i have been able to update the trasaction status to 'paid' but no clue on how to do this for the dates. I want to do this automatically.

this is what i tried

$tnx_ref = $_SESSION['genref'];
$sql = "UPDATE transactions ".
       "SET Transaction_Status = 'paid', Start_Date = 'start', Expiry_Date ='expiry'".
       "WHERE tnx_ref = $tnx_ref ";

with this the transaction status was updated to paid , startdate to start and expirydate to expiry

any explaination on how i could go about this

Upvotes: 0

Views: 1240

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44874

You can use date_add and curdate functions as

UPDATE transactions 
SET 
Transaction_Status = 'paid', 
Start_Date = curdate(),
Expiry_Date =date_add(curdate(),interval 1 year)
WHERE tnx_ref = $tnx_ref

Update from the comment "for the expiry date what if i also wanted to minus a day."

   UPDATE transactions 
    SET 
    Transaction_Status = 'paid', 
    Start_Date = curdate(),
    Expiry_Date =date_sub(date_add(curdate(),interval 1 year),interval 1 day)
    WHERE tnx_ref = $tnx_ref

Upvotes: 2

Related Questions