<?php
// Start or resume the session
session_start();

// Include the database configuration file
include 'config.php';

// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Prepare data for insertion
    $name = $_POST['name'];
    $phone = $_POST['phone'];

    // Initialize user_id
    $user_id = null;

    // Perform database query to insert user information into user_info table
    $sql1 = "INSERT INTO user_info (name, phone) VALUES ('$name', '$phone')";

    // Execute the first query
    if ($conn->query($sql1) === TRUE) {
        // Get the user ID of the inserted record
        $user_id = $conn->insert_id;
    } else {
        // Error handling for database insertion failure
        echo "Error: " . $sql1 . "<br>" . $conn->error;
        exit(); // Stop further execution if there's an error
    }

    // Check if user_id is not null
    if ($user_id !== null) {
        // Store user_id in session
        $_SESSION['user_id'] = $user_id;

        // Perform database query to check if user_id already exists in user_info2 table
        $check_query_user_info2 = "SELECT user_id FROM user_info2 WHERE user_id = '$user_id'";
        $result_user_info2 = $conn->query($check_query_user_info2);

        if ($result_user_info2) {
            if ($result_user_info2->num_rows > 0) {
                // User already exists in user_info2 table, update the record
                $update_query = "UPDATE user_info2 SET name = '$name',  phone = '$phone' WHERE user_id = '$user_id'";

                if ($conn->query($update_query) === TRUE) {
                    // Output success message
                    echo "User information updated successfully.";
                } else {
                    // Error handling for database update failure
                    echo "Error: " . $update_query . "<br>" . $conn->error;
                }
            } else {
                // Perform database query to insert user information into user_info2 table
                $sql2 = "INSERT INTO user_info2 (user_id, name, phone) VALUES ('$user_id', '$name', '$phone')";

                // Execute the second query
                if ($conn->query($sql2) === TRUE) {
                    // Output success message
                    echo "User information inserted successfully.";
                } else {
                    // Error handling for database insertion failure
                    echo "Error: " . $sql2 . "<br>" . $conn->error;
                }
            }
        } else {
            // Error handling for database query failure
            echo "Error: " . $check_query_user_info2 . "<br>" . $conn->error;
        }
    } else {
        // Error handling if user_id is null
        echo "Error: User ID is null.";
    }

    // Redirect to home.php after processing the form
    header("Location: home.php");
    exit(); // Ensure no further code execution after redirection
}
?>



<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="utf-8">
	<title>Form-v6 by Colorlib</title>
	<!-- Mobile Specific Metas -->
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<!-- Font-->
	<link rel="stylesheet" type="text/css" href="css/nunito-font.css">
	<!-- Main Style Css -->
    <link rel="stylesheet" href="css/style.css"/>

<style>
    #bg-video {
    width: 100%; /* Set the width to 50% of its container */
    /* You can also specify specific width and height values */
    /* width: 400px; */
    /* height: 300px; */
}

</style>
   
</head>


<body class="form-v6">
	<div class="page-content">
		<div class="form-v6-content">


	<div class="form-left">
      <video autoplay muted loop id="bg-video">
        <source src="mp3.mp4" type="video/mp4">
        <!-- Add additional source elements for different video formats if needed -->
        Your browser does not support the video tag.
    </video>
</div>



<div class="form-detail">
    <h2>Enter Your Information</h2>
    <form id="userInfoForm" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
        <div class="form-row">
            <label for="name">Name:</label>
            <input type="text" id="name" name="name" class="input-text" placeholder="Your Name" required>
        </div>
    
        <div class="form-row">
            <label for="phone">Phone Number:</label>
            <input type="text" id="phone" name="phone" class="input-text" placeholder="Phone Number" required>
        </div>
        <div class="form-row-last">
            <input type="submit" value="Submit" class="register" name="register">
        </div>
    </form>
</div>



    <script>
        function startOrder() {
            // Show the spinner
            document.getElementById('spinner').style.display = 'block';
            
            // Redirect to the order page after a delay
            setTimeout(function() {
                // Assuming you want to submit the form data to another page
                document.getElementById('userInfoForm').submit();
            }, 2000); // Adjust the delay time as needed (in milliseconds)
        }
    </script>

 
</body>
</html>
