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