sinesine sinesine - 25 days ago 8
SQL Question

SQL Syntax Error in PHP code - adding together two values

I keep getting this syntax error in my MySQL code within a PHP file. I'm simply trying to increment/add to the value already in the table with this time variable. If anyone could help me out I'd greatly appreciate it.

PHP:

$sql = "UPDATE Aircraft
SET MaintenanceFlightTime = (MaintenanceFlightTime + $MaintenanceDuration),
WHERE AircraftID = $AircraftID";


Error:

UPDATE Aircraft SET MaintenanceFlightTime = (MaintenanceFlightTime + 00:10:00), WHERE AircraftID = 8
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':10:00), WHERE AircraftID = 8' at line 2

Answer

You cannot just add a string like '01:01:01' to a time column but you can use ADDTIME()

$MaintenanceDuration = '00:10:00';

$sql = "UPDATE Aircraft
        SET MaintenanceFlightTime = ADDTIME(MaintenanceFlightTime, '$MaintenanceDuration')
        WHERE AircraftID = $AircraftID";
Comments