gula Posted April 10, 2023 Share Posted April 10, 2023 <?php include("connection.php"); // Check if the form is submitted if (isset($_POST['submit'])) { // Retrieve the user's information from the form $Bus_id =$_POST['Bus_id']; $city = $_POST['city']; $Destination = $_POST['Destination']; $bus_number = $_POST['Bus_number']; $departure_date = $_POST['departure_date']; $departure_time = $_POST['departure_time']; $cost = $_POST['cost']; $seat_id = $_POST['seat_id']; $fullName = $_POST['fullName']; $contactNumber = $_POST['contactNumber']; $email = $_POST['email']; $gender = $_POST['gender']; // Prepare and execute the sql query to insert the user's information into the "bookings" table $stmt = mysqli_prepare($conn, "INSERT INTO `bookings` (Bus_id, city, Destination, Bus_number, departure_date, departure_time, cost, seat_id, fullName, contactNumber, email, gender) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); // Bind the variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "ssssssssssss", $bus_id, $city, $destination, $bus_number, $departure_date, $departure_time, $cost, $seat_id, $fullName, $contactNumber, $email, $gender); if (mysqli_stmt_execute($stmt)) { echo "Booking Successful!"; } else { echo "Booking Failed!"; } // Close the database connection mysqli_close($conn); } if (isset($_POST['seat_id'])) { $seatId = $_POST['seat_id']; // Check if the seat is available $query = "SELECT * FROM seats WHERE seat_id = '$seatId' AND is_booked = 0"; $result = mysqli_query($conn, $query); if (mysqli_num_rows($result) > 0) { // Book the seat $query = "UPDATE seats SET is_booked = 1 WHERE seat_id = '$seatId'"; mysqli_query($conn, $query); $message = 'Seat booked successfully'; } else { echo 'Seat is already booked'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/316116-why-is-my-data-not-inserting-into-the-database/ Share on other sites More sharing options...
TheStudent2023 Posted April 10, 2023 Share Posted April 10, 2023 @gula Put this code at the top of the page and then try submitting. See if you get any errors. If so, copy & paste here: ```` ini_set("display_errors",1); ini_set("display_startup_errors",1); error_reporting(E_ALL); ```` Quote Link to comment https://forums.phpfreaks.com/topic/316116-why-is-my-data-not-inserting-into-the-database/#findComment-1607189 Share on other sites More sharing options...
gizmola Posted April 10, 2023 Share Posted April 10, 2023 People can not help you debug if you don't provide information about the failure. How is it that you know it doesn't insert in the database? Is it because there's a 500 error, or the script runs and displays "Booking failed" or what? You don't provide the form code, so that could be an issue. Probably the statement execution is failing with a sql error but you don't capture/log the error. Beginners seem to always do what you are doing and create a bunch of variables that just copy values from the superglobal for no reason. $Bus_id =$_POST['Bus_id']; $city = $_POST['city']; $Destination = $_POST['Destination']; //etc Several things jump out at me: The $_POST array is already an array variable, and you can use it to pass the values. Don't make a bunch of other variable names for no reason There is an established variable naming standard for php. It's camelcase. Don't make a variable named $Bus_id. It should be $busId. $Destination should be $destination. Be consistent with your html attribute naming. You are all over the place. You have 'Bus_id' and 'city' and 'Destination'. The most commonly accepted convention is "all lowercase, with words separated either by a '-' or an '_'. I would suggest underscore as html is often manipulated later with javascript, and the '-' is a symbol in javascript, but otherwise choose which one you like. So fixing your form input names 'bus_id' 'city' 'destination' The other question that arises in your code, is why you have a bus_id and not a city_id and destination_id? At any rate, I bring up the lack of rigor in your html and php variable naming, because lack of standards often lead to bugs that get traced back to inconsistent variable naming. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316116-why-is-my-data-not-inserting-into-the-database/#findComment-1607192 Share on other sites More sharing options...
Strider64 Posted April 11, 2023 Share Posted April 11, 2023 I suggest looking into using PDO https://phpdelusions.net/pdo as well as the code is so much cleaner in my opinion as well. Here's an example of what I'm talking about -> $sql = "INSERT INTO lego_trivia (points, question, answer, canvas_images) VALUES (:points, :question, :answer, :canvas_images)"; $stmt = $pdo->prepare($sql); // Bind the values to the placeholders $stmt->bindValue(':points', $points, PDO::PARAM_INT); $stmt->bindValue(':question', $question); $stmt->bindValue(':answer', $answer); $stmt->bindValue(':canvas_images', $savePath); // Execute the prepared statement $insertSuccess = $stmt->execute(); Quote Link to comment https://forums.phpfreaks.com/topic/316116-why-is-my-data-not-inserting-into-the-database/#findComment-1607203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.