Unable to Establish Connection Between HTML Form and Database Using PHP

I’m currently working on a project where I have an HTML form that collects user input, and I want to store this data in a database(fina_database) using PHP. However, I’m encountering difficulties in establishing a connection between my HTML form and the database. Here’s what I have so far:

HTML Form (Registration-form-page.html):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Registration Form</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="login-form">
        <div class="container">
            <div class="alert" id="alertBox">
                <span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
                <span id="alertMessage"></span>
            </div>
            <div class="main">
                <div class="content">
                    <div class="header-form">
                        <h2>Registration Form</h2>
                        <p class="account">Already Have An Account? <a href="#">Login</a></p>
                    </div>
                    <form id="registrationForm" action="123.php" method="post">
                        <div class="name-fields">
                            <input type="text" name="first_name" placeholder="First Name" required>
                            <input type="text" name="last_name" placeholder="Last Name" required>
                        </div>
                        <input type="date" name="birthday" placeholder="Birthday" required>
                        <input type="email" name="email" placeholder="Email" required>
                        <input type="password" name="password" placeholder="User Password" required>
                        <input type="password" name="confirm_password" placeholder="Confirm Password" required>
                        <div class="gender-fields">
                            <label>Gender:</label>
                            <input type="radio" id="male" name="gender" value="m" required>
                            <label for="male">Male</label>
                            <input type="radio" id="female" name="gender" value="f" required>
                            <label for="female">Female</label>
                        </div>
                        <button class="btn" type="submit">Register</button>
                    </form>
                </div>
                <div class="form-img">
                    <img src="https://media.giphy.com/media/l4KhKRcaYb43LVqq4/giphy.gif" alt="Running Bird GIF">
                </div>
            </div>
        </div>
    </div>
    <script src="script.js"></script>
</body>
</html>

PHP Script (123.php):

<?php
// Database connection
$servername = "localhost";
$username = "root"; 
$password = " "; 
$database = "final_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// If form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Escape user inputs to prevent SQL injection
    $first_name = $conn->real_escape_string($_POST['first_name']);
    $last_name = $conn->real_escape_string($_POST['last_name']);
    $birthday = $conn->real_escape_string($_POST['birthday']);
    $email = $conn->real_escape_string($_POST['email']);
    $password = $conn->real_escape_string($_POST['password']);
    $confirm_password = $conn->real_escape_string($_POST['confirm_password']);
    $gender = $conn->real_escape_string($_POST['gender']);

    // Check if passwords match
    if ($password != $confirm_password) {
        echo "Passwords do not match!";
    } else {
        // Insert data into database
        $sql = "INSERT INTO users (first_name, last_name, birthday, email, password, gender)
        VALUES ('$first_name', '$last_name', '$birthday', '$email', '$password', '$gender')";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

// Close connection
$conn->close();
?>

sql script (final_database.sql):

CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    birthday DATE NOT NULL,
    email VARCHAR(100) NOT NULL,
    password VARCHAR(255) NOT NULL,
    gender ENUM('m', 'f') NOT NULL
);

When I submit the form, it redirects to process.php as expected, but no data is inserted into the database. I’ve checked my database credentials, and they are correct. Can anyone help me identify what might be causing this issue? Thank you in advance!

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật