I have a form, that sends multiple seats to a php file. I created a foreach loop to insert each seat into sql. For some reason i am able to get all the information i need to send it but it wont insert.
I dont know if it has something to do with foreign keys? But what i want to do is show what email selected what seat. I also want to disable each seat when it is sent to sql.
This is my output.
You are currently in database admin.
admin@airways.com
1A
1B
1C
Data added successfully.
Back to seating.
This is my flightselect.php
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
if (login_check($mysqli) == true) {
$logged = 'in';
} else {
$logged = 'out';
}
$username = $_SESSION['username'];
echo '<p>You are currently '.$logged. ' database '.$username. '.</p><br>';
$getemail = mysqli_query($mysqli, "SELECT email FROM members WHERE username = '$username' ");
$email = '';
while($gear = mysqli_fetch_array( $getemail )) {
echo $gear['email'];
$email = $gear['email'];
}
echo $gear."<br>";
if(isset($_POST['seatme'])){
if(!empty($_POST['flight'])){
foreach($_POST['flight'] as $key=>$selected){
echo $selected."<br>";
$result = mysqli_query($mysqli, "INSERT INTO seating (seat_id, email, flight_id)
VALUES ('$selected', '$gear', '1' )");
}
}
}
echo "<font color='green'>Data added successfully.";
echo "<br/><a href='user.php'>Back to seating.</a>";
?>
You need a brackets after the VALUES keyword...
if (!mysqli_query($mysqli, "INSERT INTO seating (seat_id, email, flight_id)
VALUES ('$selected', '$gear', '1' )")) {
printf("Error: %s
", $mysqli->error);
}
You should also learn about prepared statements as they make your SQL more secure.