Creating a Login Page with request to MySQL
Welcome to our tech support site! In this article, we will guide you through the process of creating a login page with a request to MySQL. This login page will allow users to securely access their accounts on your website.
Prerequisites
Before we begin, make sure you have the following:
- A web server installed on your local machine or a hosting provider.
- MySQL database server installed.
- A basic understanding of HTML, CSS, and PHP.
Step 1: Create the Login Form
The first step is to create the login form where users can enter their credentials. Open your favorite text editor and create a new HTML file named login.html. Copy and paste the following code:
<form action="login.php" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br>
<input type="submit" value="Login">
</form>
This code creates a simple login form with fields for the username and password. The form's action attribute is set to login.php, which is the file where we will handle the login request.
Step 2: Handle the Login Request
Now, let's create the login.php file to handle the login request. Create a new PHP file named login.php and add the following code:
<?php
// Retrieve user input from the login form
$username = $_POST['username'];
$password = $_POST['password'];
// Connect to the MySQL database
$conn = mysqli_connect('localhost', 'your_username', 'your_password', 'your_database');
// Check if the connection was successful
if (!$conn) {
die('Could not connect to MySQL: ' . mysqli_connect_error());
}
// Create a query to check if the user exists in the database
$query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);
// Check if the query was successful
if (mysqli_num_rows($result) == 1) {
echo "Login successful! Welcome, $username!";
} else {
echo "Invalid username or password.";
}
// Close the database connection
mysqli_close($conn);
?>
This code retrieves the username and password entered by the user and connects to the MySQL database using the provided credentials. It then executes a query to check if the user exists in the database. If the query returns a row, it means the login was successful, and the user is welcomed. Otherwise, an error message is displayed.
Step 3: Test the Login Page
Now that we have created the login form and the PHP login script, it's time to test our login page. Save both files in the same directory and open the login.html file in your web browser. Enter the credentials of a user that exists in your database and click the "Login" button.
If everything is set up correctly, you should see a message saying "Login successful! Welcome, [username]!" If the credentials are incorrect, you will see the message "Invalid username or password."
Congratulations! You have successfully created a login page with a request to MySQL. This login page allows users to securely access their accounts on your website. Feel free to customize the login form and the PHP script to fit your specific requirements.
References
| Source | Description |
|---|---|
| w3schools - HTML Tutorial | Learn HTML basics and create web pages. |
| w3schools - CSS Tutorial | Learn CSS to style your web pages. |
| w3schools - PHP Tutorial | Learn PHP programming language. |
| MySQL Documentation | Official MySQL documentation for reference. |