INSERT DATA IN TWO TABLES WITH FOREIGN KEY

Viewed 12

Need some help bout this. I want to insert a data to patients table and bookings table. The patient_id from patients is an Autoincrement number. How do I use the ID to insert it as a foreign key to bookings table ?

Here is my code:

<?php 
        if(isset($_POST['submit'])){
            $name=$_POST['name'];
            $email=$_POST['email'];
            $gender=$_POST['gender'];
            $birthdate=$_POST['birthdate'];
            $address=$_POST['address'];
    
            // FOR PATIENT TABLE
            $sql_patient = "insert into `patients` (patient_name,patient_email,patient_gender,patient_birthdate,patient_address)
            values('$name','$email','$gender','$birthdate','$address')";
            $result_patient = mysqli_query($con, $sql_patient);
            

            // FOR BOOKINGS TABLE
            $book_date=$_POST['bookdate'];
            $book_time=$_POST['booktime'];
            $booking_purpose=$_POST['purpose'];
            $sql_bookings ="insert into `bookings` (booking_date,booking_time,booking_purpose,patient_id)
            values('$book_date','$book_time','$booking_purpose')";
            $result_bookings = mysqli_query($con, $sql_bookings);
            if($result_patient){
    ?> 
                <script src="js/sweetalert.min.js"></script>
                <script>
                    swal({
                    title: "Successfully!",
                    text: "Congrats, You have succesfully booked an Appointment",
                    icon: "success",
                    button: "Okay",
                    });
                </script>
    <?php
            }else{
                die(mysqli_error($con));
            }
        }  
    ?>
0 Answers
Related