I am a bit confused with my following code to insert into my database using PHP. I used this code a few days ago and it was working fine and i tried it now and it was not working.
This is the PHP code:
<?php
session_start();
require_once('dbconn.php');
$user = $_SESSION['who'];
if (!$_SESSION['who'])
{
header("location: login.php");
}
if (isset($_POST['submit']))
{
if (isset($_POST['flights'])){
$flightID = $_POST["flights"];
}
date_default_timezone_set('Australia/Sydney');
$date = date("Y-m-d h:i:s");
if (isset($_POST['baggage']))
{
$baggage = $_POST['baggage'];
}
$sql = "INSERT into booking (flight_id,customer_id,booking_datetime,baggage)
values ('$flightID','$user','$date','$baggage')";
$result = mysqli_query($dbConn, $sql) or trigger_error("Query Failed! SQL: $sql - Error: ".mysqli_error($dbConn), E_USER_ERROR);
}
?>
After debugging the code it appears that my flightID variable contains nothing. This was the error
"Query Failed! SQL: INSERT into booking (flight_id,customer_id,booking_datetime,baggage) values ('','10','2020-01-28 01:28:13','121') - Error: Incorrect integer value: '' for column 'flight_id' "
This is my html for the flightID field
<div>
<label for="flights"> choose a flight:</label> <br>
<select name = "flights ">
<option id = "flights" name = "flights" value = "1"> QF1769: cape york -> darwin </option>
<option id = "flights" name = "flights" value = "2"> QF1988: cape york -> QLd </option>
<option id = "flights" name = "flights" value = "3"> QF1654: cape york -> melbourne </option>
</select>
<span class="error" id="flightRequired">flight required</span>
</div>