0

How do I update the value of status with the current date and time after clicking the complete button?

Example:

Status = 02/05/2019 8:54 AM

So far this is what I've tried:

<?php

  if(isset($_POST['completed']))
    {
        $msg = "Completed";
        $status = $_POST['completed'];
    }

    $reqnumber=$_POST['reqnumber'];
    $con = mysqli_connect('localhost', 'root', '');
    mysqli_select_db($con, 'pcrequest');

    $sql = "UPDATE request SET status = '$status', status = 'date('Y-m-d H:i:s');' WHERE reqnumber = '$reqnumber'";

    if(mysqli_query($con, $sql))
        header("refresh:1; url=messages.php?msg=$msg");
    else
        var_dump(mysqli_error($con));
 
?>
  • 1
    What is the table structure of `request`? Do you want all its rows updated? `UPDATE request SET status = NOW() WHERE ...` is a better way. – danblack Feb 05 '19 at 01:02
  • 1
    There is a similar question https://stackoverflow.com/questions/19246309/how-to-get-current-date-time-in-mysql As correctly indicated by @danblack, setting status= NOW() will suffice. – Vaibhav Gupta Feb 05 '19 at 01:05
  • Why not have a column which simply stores the current_timestamp whenever a row is updated? – Strawberry Feb 05 '19 at 08:04

1 Answers1

0

You are trying to set "status" twice. That semicolon and nested single quotes could cause some issues as well where you have the date() function.

I would recommend setting the date outside of the query and inserting it as a variable, like this:

$now = date('Y-m-d H:i:s');

$sql = "UPDATE `request` 
    SET `status` = '$now' 
    WHERE `reqnumber` = '$reqnumber';";
tshimkus
  • 1,173
  • 2
  • 17
  • 24