SELECT from MySQL, use modified data from a column to UPDATE a new column in the same table

55 views Asked by At

I'm using the jQuery jeditable to allow editing an HTML table based on a MySQL database table. But its not the issue. It works great on all of the columns except one, a timeout column. I want to grab the date part of the logdate column and use it to build the timeout value. The new hour:min part is what the user has entered. The column being edited is called "timeout".

My first attempt looked like this;

if ($column == "timeout") {
    $toSQL = "UPDATE NetLog t2
        JOIN NetLog t1 ON t2.recordID = t1.recordID
        SET t2.timeout = DATE_FORMAT(t2.logdate, '%Y-%m-%d')
        WHERE t2.recordID = $recordID";
    $stmt = $db_found->prepare($toSQL);
    $stmt->execute();
}

This did not update the table and it did not throw any errors. And yes $value resolves to 12:34 or whatever was entered by the user. Making the new timeout value 2017-10-20 12:34.

Then I tried this;

If ($column == "timeout") { $TOSQL = "SELECT DATE_FORMAT(logdate,
 '%Y-%m-%d') as dateonly, recordID
     FROM NetLog 
     WHERE recordID = $recordID"; foreach($db_found->query($TOSQL) as $row) {   $newdttm  = "$row[dateonly] $value";    $recordID =
 "$row[recordID]"; }    $moretogo = 1; }         if ($column == "timeout" AND
 $moretogo == 1) {  echo("$newdttm $recordID "); // 2017-10-20 12:34
 8117   $sql = "UPDATE NetLog SET timeout = '$newdttm' 
                             ,timeonduty =  (timestampdiff(SECOND, logdate, '$newdttm'))            WHERE recordID = $recordID "; $stmt = $db_found-prepare($sql);
 $stmt->execute(); }

The output of the echo looks like this: 2017-10-20 12:34 8117, which is exactly what I would expect. I've tried quite a few other things too but nothing I do updates the MySQL table. This throws a 500 error but I can't find anything wrong to cause it.

0

There are 0 answers