sinesine sinesine - 1 month ago 8
PHP Question

Time variable - PHP not updating database field

I've got a form that allows the user to input an ID and a duration. If the duration is less than or equal to an hour, then 10 minutes are added to a field called

MaintenanceFlightTime
, If the duration is more than an hour, 15 minutes are added.
MaintenanceFlightTime
and
ID
are in a separate table to where
Duration
is stored, however
ID
is a foreign key in
Duration
's table.

I've got three files: the form, the insertion script for
ID
and
Duration
, and the updating script for
MaintenanceFlightTime
. The insertion script works fine but the updating script simply isn't working.

Form:

<html>
<head>
<meta charset="UTF-8">
<title>Add Flight</title>
</head><body>
<form action="insert.php" method="post">
<p>
<label for="ID">ID:</label>
<input type="text" name="ID" id="ID">
</p>
<p>
<label for="Duration"> Duration:</label>
<input type="time" name="Duration" id="Duration">
</p>
<input type="submit" value="Submit">
</form>
</body>
</html>


Insert
ID
&
Duration
:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$ID = $_POST['ID'];
$Duration = $_POST['Duration'];

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO InfoTable (ID, Duration)
VALUES ('$ID', '$Duration')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

include 'maintenance.php';

$conn->close();


Calculate & Update
MaintenanceFlightTime
:

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$parsedDuration = explode(':', $Duration);

$hours = intval($parsedDuration[0]);
$mins = intval($parsedDuration[1]);

if ($hours > 0 && $mins > 0) {
echo "Added 15 maintenance minutes to $ID!";
$MaintenanceDuration = strtotime('0:15:00');
} else {
echo "Added 10 maintenance minutes to $ID!";
$MaintenanceDuration = strtotime('0:10:00');
}

if ($conn->query($sql) === TRUE) {
echo "Maintenance added successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$MaintenanceDuration = strftime('%H:%M', $MaintenanceDuration);
echo $MaintenanceDuration;

$sql = "UPDATE MaintenanceTable
SET MaintenanceFlightTime = MaintenanceFlightTime + $MaintenanceDuration
WHERE ID = $ID";


Thanks in advance for any help.

Answer

You can try this:

$hours = intval($parsedDuration[0]);
$mins  = intval($parsedDuration[1]);

if ($hours > 0 && $mins > 0) {
    echo "Added 15 maintenance minutes to $ID!";
    $MaintenanceDuration = 15;
} else {
    echo "Added 10 maintenance minutes to $ID!";
    $MaintenanceDuration = 10;
}

echo $MaintenanceDuration;

$sql = "UPDATE MaintenanceTable SET MaintenanceFlightTime
=(MaintenanceFlightTime + INTERVAL $MaintenanceDuration MINUTE)
WHERE ID = $ID";
Comments