时间变量 - PHP不更新数据库字段

时间:2016-10-20 20:58:13

标签: php

我有一个允许用户输入ID和持续时间的表单。如果持续时间小于或等于一小时,则将10分钟添加到名为MaintenanceFlightTime的字段中,如果持续时间超过一小时,则添加15分钟。 MaintenanceFlightTimeID位于存储Duration的单独表格中,但IDDuration表格中的外键。

我有三个文件:表单,IDDuration的插入脚本,以及MaintenanceFlightTime的更新脚本。插入脚本工作正常,但更新脚本无法正常工作。

形式:

<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>

插入ID&amp; 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();

计算&amp;更新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";

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

$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";