SobanSA SobanSA - 2 months ago 24
MySQL Question

Undefined Index in PHP Forms

I recently started learning PHP on my own from w3schools.com for my university project. When using PHP and MySQL Forms, in the beginning I succeeded in saving inputs into the database but then suddenly I starded receiving following error an I have searched for solutions on internet but could not get any idea.


( ! ) Notice: Undefined index: flight_id in C:\wamp\www\alpha_airlines\admin.php on line 64

( ! ) Notice: Undefined index: flight_time in C:\wamp\www\alpha_airlines\admin.php on line 65

Same for every input.


Here goes my code:



//HTML FORM

<table align="center">
<tr><td colspan="2" align="center"><h2>Add a Flight</h2></td></tr>
<form name="add-flight" method="post">
<tr>
<td>Flight ID: </td><td><input type="text" class="textbox" name="flight_id" maxlength="7"></td>
</tr>
<tr>
<td>Flight Time: </td><td><input type="time" class="textbox" name="flight_time"> </td>
</tr>
<tr>
<td>Flight Date: </td><td><input type="date" class="textbox" name="flight_date"> </td>
</tr>
<tr>
<td>Flight From: </td><td><input type="number" class="textbox" name="flight_from" maxlength="2"> </td>
</tr>
<tr>
<td>Flight To: </td><td><input type="number" class="textbox" name="flight_to" maxlength="2"> </td>
</tr>
<tr>
<td>Airplane ID: </td><td><input type="text" class="textbox" name="plane_id" maxlength="5"> </td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Add Flight" class="button" name="submit_flight"> </td>
</tr>
</form>
</table>
//PHP CODE

<?php

$flightid = $_POST['flight_id'];
$flighttime = $_POST['flight_time'];
$flightdate = $_POST['flight_date'];
$flightfrom = $_POST['flight_from'];
$flightto = $_POST['flight_to'];
$planeid = $_POST['plane_id'];

$sql = "INSERT INTO flight (Flight_ID, Flight_Time, Flight_date, Flight_From, Flight_To, Airplane_ID)
VALUES ('$flightid', '$flighttime', '$flightdate' , '$flightfrom' , '$flightto' , '$planeid')";

if (mysqli_query($conn, $sql)) {
echo "New flight created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>




Answer

You must check $_POST that is empty or not.

First time that page loaded $_POST is empty and when you submit form with click on Add Flight ، $_POST set with flight_id,flight_time,....

like this

if(!empty( $_POST['flight_id'])){   
    $flightid = $_POST['flight_id'];
    $flighttime = $_POST['flight_time'];
    $flightdate = $_POST['flight_date'];
    $flightfrom = $_POST['flight_from'];
    $flightto = $_POST['flight_to'];
    $planeid = $_POST['plane_id'];

    $sql = "INSERT INTO flight (Flight_ID, Flight_Time, Flight_date, Flight_From, Flight_To, Airplane_ID)
    VALUES ('$flightid', '$flighttime', '$flightdate' , '$flightfrom' , '$flightto' , '$planeid')";

    if (mysqli_query($conn, $sql)) {
        echo "New flight created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);
}
Comments