How to Create a Registration Form in PHP and MySQL
Tags: webdev, php, mysql, backend
Published: 2024-03-19 06:13:03

## How to Create a Registration Form in PHP

Follow these steps to learn How to Create a Registration Form in PHP and MySQL in 2023.

1. Setting up the Environment
2. Create a new database in PHPMyAdmin on localhost
3. Setting up the database
4. Create a simple HTML form
5. PHP validation and submission
6. Storing user data in MySQL database
7. Write PHP code for the user registration form
8. Successful user 
registration confirmation. 

You can also watch the video tutorial for the step-by-step process of creating a new database on localhost. Also, learn How to Create a Registration Form in PHP and MySQL.

## Video Tutorial – How to Create a Registration Form in PHP

{% embed https://youtu.be/b5ruAhqU17o %}


## Setting Up the Database
The first step in creating a user registration form is to set up a database to store user information. To do this, we’ll use MySQL, a popular relational database management system. Here’s a sample SQL query for creating a table to store user information:

```
CREATE TABLE users (
  id INT(11) AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(50) NOT NULL UNIQUE,
  email VARCHAR(50) NOT NULL UNIQUE,
  password CHAR(60) NOT NULL
);
```
This query creates a table called “users” with four columns: an auto-incrementing “id” column that serves as the primary key, a “username” column for storing the user’s chosen username, an “email” column for storing the user’s email address, and a “password” column for storing the user’s hashed password.

## Creating the HTML Form
The next step is to design the user interface of the registration form. You can use HTML to create the form and CSS to style it. Make sure that the form is user-friendly and easy to navigate.

```
<?php include('register.php'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <title>User Registration Form</title>
    <style>
        body {
            background-color: #eeeeee;
            font-family: Arial, sans-serif;
            padding: 80px;
        }
        form {
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0px 0px 10px #ccc;
            padding: 20px;
            width: 350px;
            margin: 0 auto;
        }
        input[type=text],
        input[type=password],
        input[type=email] {
            width: 100%;
            padding: 12px 20px;
            margin: 8px 0;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button[type=submit] {
            background-color: #4CAF50;
            color: white;
            padding: 14px 20px;
            margin: 8px 0;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            width: 100%;
        }
        button[type=submit]:hover {
            background-color: #45a049;
        }
        #successMessage {
            color: green;
            font-size: 15px;
            margin: 10px;
        }
        #errorMessage {
            color: red;
            font-size: 15px;
            margin: 10px;
        }
    </style>
</head>
<body>
    <form method="POST">
        <h2>User Registration Form</h2>
        <label for="username">Username:</label>
        <input type="text" id="username" name="username" required>
        <label for="email">Email:</label>
        <input type="email" id="email" name="email" required>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password" required>
        <!-- Display success and error messages -->
        <div id="errorMessage"> <?php echo $error  ?> </div>
        <div id="successMessage"> <?php echo $success  ?> </div>
        <button type="submit" name="button">Register</button>
    </form>
</body>
</html>
```

## PHP Validation and Submission
Now that we have a form that collects user information, we need to create a PHP script that validates the submitted data and inserts it into the MySQL database.

## Validating User Input
After designing the form, it’s important to validate user input. Server-side validation ensures that user input is correct and meets the required criteria. In PHP, you can use the “filter_input()” function to validate user input.

PHP code for validating user input should be added before inserting the data into the database. You can also display error messages to guide users in filling out the form correctly.

## Storing User Data in MySQL
Once the user input is validated, the next step is to store user data in a MySQL database. To do this, create a PHP file to connect to MySQL and insert user data into the database. You can use the “mysqli” function in PHP to connect to the MySQL database.

## User Registration – PHP Code

Here’s an example of what the PHP script might look like: Create a new PHP file name register.php and paste the below code inside this.

```
<?php
// Connect to the MySQL database
$success = "";
$error = "";
if (array_key_exists("button", $_POST)) {
$mysqli = new mysqli("localhost", "root", "", " Your Database name");
// Check for errors
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
} else { 
  // Validate the submitted data
  $username = $_POST["username"];
  $email = $_POST["email"];
  $password = password_hash($_POST["password"], PASSWORD_DEFAULT);
  // Check if the username or email already exists in the database
  $result = $mysqli->query("SELECT * FROM users WHERE username='$username' OR email='$email'");
  if ($result->num_rows > 0) {
      $error = "Username or email already taken.";
  } else {
    // Insert the user's information into the database
    $mysqli->query("INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')");
     
    //success message
      $success = "Data submitted Successfully";
  }
}
}
?>
```

**Conclusion**
In this blog post, we have shown you how to create a registration form in PHP and MySQL. Following these steps, you can easily collect user data and store it securely in a database. Registration forms are an essential component of any website that requires users to create accounts or sign up for a service. So, start creating your registration form today and start collecting user data!