提取sql查询并将其与使用php

时间:2019-01-19 13:07:13

标签: javascript php mysql database

我正在用PHP做一个项目,我需要将航班时刻表(从机场ID到机场ID)与其他字段一起添加到数据库中,同时在数据库中添加第二个条目时,它必须检查先前添加的数据并且如果当时没有计划起飞或到达的航班,则只需存储一个新字段。我的PHP代码如下:

<?php

$host = 'localhost';
$username = 'root';
$password = 'root';
$dbname = 'assignment';

$conn = mysqli_connect($host, $username, $password, $dbname);
if (!$conn) {
    die("Error connecting to the database");
}

$id = $_POST['id'];
$from_airport_id = $_POST['from_airport_id'];
$to_airport_id = $_POST['to_airport_id'];
$flight_no = $_POST['flight_no'];
$depart_time = $_POST['depart_time'];
$arrival_time = $_POST['arrival_time'];

$sql1 = "SELECT * FROM flight_schedule";

$result = mysqli_query($conn, $slq1);

$sql = "INSERT INTO flight_schedule (id, from_airport_id, to_airport_id, flight_no, depart_time, arrival_time) 
    VALUES ('$id','$from_airport_id', '$to_airport_id', '$flight_no', '$depart_time', '$arrival_time')";

if (mysqli_num_rows($result)) {
    while ($row = mysqli_fetch_assoc($result)) {
        if ($row['from_airport_id'] == $from_airport_id or $row['to_airport_id'] == $to_airport_id) {
            if ($row['depart_time'] != $depart_time and $row['depart_time'] != $arrival_time and $row['arrival_time'] != $depart_time and $row['arrival_time'] != $arrival_time) {
                if (mysqli_query($conn, $sql)) {
                    echo 'Inserted';
                } else {
                    echo 'Not Inserted';
                }
            } else {
                echo 'Error:' . $row['flight_no'] . 'is already scheduled at that time' . mysqli_error();
            }
        } else {
            if (mysqli_query($conn, $sql)) {
                echo 'New values inserted successfully';
            } else {
                echo 'Error!!';
            }
        }
    }
}

mysqli_close($conn);

0 个答案:

没有答案