-1

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>
Tim Morton
  • 2,614
  • 1
  • 15
  • 23
William Awad
  • 109
  • 8
  • I can’t tell where blocks line up, need to fix indentation – Tim Morton Jan 28 '20 at 02:42
  • try typecasting $flightID to (int) – Juakali92 Jan 28 '20 at 02:54
  • 1
    Based on the error message, it looks like `$flightID` is empty. Could be due to the extra space in the `name` attribute: ` – showdev Jan 28 '20 at 02:55
  • 1
    You shouldn't have multiple `id="flights"` (why do you think the options need IDs in the first place?). And there's no need to give a name to options, only the ` – Barmar Jan 28 '20 at 03:03
  • Your code is wide open to SQL-injection, you should learn to use prepared statements. See https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 – Barmar Jan 28 '20 at 03:04
  • After replacing spaces for tabs, I was able to see how your `if` blocks align. I noticed that you are avoiding assigning variables if the post value is not set; but you’re not setting a default value so if there is no post value, you’re still going to have an undefined variable. That doesn’t address your current problem though. Right after you check for submission, `print_r($_POST)` and find out if flights is in the array. That way you know which direction to look. You might check that space in `name="flights "`. I don’t think that’s it, but you never know – Tim Morton Jan 28 '20 at 03:26

1 Answers1

0
 <div>
  <label for="flights"> choose a flight:</label> <br>
  <select name="flights">
    <option id="flights1" value="1"> QF1769: cape york -> darwin </option>
    <option id="flights2" value="2"> QF1988: cape york -> QLd </option>
    <option id="flights3" value="3"> QF1654: cape york -> melbourne 
    </option>
  </select>

  <span class="error" id="flightRequired">flight required</span>

 </div>
Marmik Patel
  • 192
  • 2
  • 12