Jump to content

why is my data not inserting into the database


gula

Recommended Posts

<?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';

    }

  }

?>

 

Link to comment
Share on other sites

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.

  • Thanks 1
Link to comment
Share on other sites

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();

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.