Signup form using PHP and MySQL Database Last Updated : 09 Sep, 2020 Comments Improve Suggest changes Like Article Like Report The task is to create and design a sign-up form in which if the user enters details, the HTML form data are inserted into our MySQL server database. Approach: First task is that we have to create our MySQL server Database and a Table according to our requirements. We connect our MySQL server Database using PHP mysqli_connect() function which takes four arguments, i.e. our "servername", "username", "password" and "database". Note: No CSS code is used here as we are using Bootstrap in the following PHP code. You can apply CSS and style in your web page as per your own application requirement. PHP code to create Database connection: First and the most basic step is to create a Database connection. The PHP file name used here is "dbconnect.php" and the database name used is "geeksforgeeks". php <?php $servername = "localhost"; $username = "root"; $password = ""; $database = "geeksforgeeks"; // Create a connection $conn = mysqli_connect($servername, $username, $password, $database); // Code written below is a step taken // to check that our Database is // connected properly or not. If our // Database is properly connected we // can remove this part from the code // or we can simply make it a comment // for future reference. if($conn) { echo "success"; } else { die("Error". mysqli_connect_error()); } ?> "users" table is created using MySQL phpMyAdmin tool as shown below. PHP code to design sign-up form: Now when we have successfully connected to our Database, it is time to create the Signup form for the users. The following PHP code demonstrates the sign-up form. The MySql database table name used is "users". php <?php $showAlert = false; $showError = false; $exists=false; if($_SERVER["REQUEST_METHOD"] == "POST") { // Include file which makes the // Database Connection. include 'dbconnect.php'; $username = $_POST["username"]; $password = $_POST["password"]; $cpassword = $_POST["cpassword"]; $sql = "Select * from users where username='$username'"; $result = mysqli_query($conn, $sql); $num = mysqli_num_rows($result); // This sql query is use to check if // the username is already present // or not in our Database if($num == 0) { if(($password == $cpassword) && $exists==false) { $hash = password_hash($password, PASSWORD_DEFAULT); // Password Hashing is used here. $sql = "INSERT INTO `users` ( `username`, `password`, `date`) VALUES ('$username', '$hash', current_timestamp())"; $result = mysqli_query($conn, $sql); if ($result) { $showAlert = true; } } else { $showError = "Passwords do not match"; } }// end if if($num>0) { $exists="Username not available"; } }//end if ?> <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content= "width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href= "https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity= "sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous"> </head> <body> <?php if($showAlert) { echo ' <div class="alert alert-success alert-dismissible fade show" role="alert"> <strong>Success!</strong> Your account is now created and you can login. <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> '; } if($showError) { echo ' <div class="alert alert-danger alert-dismissible fade show" role="alert"> <strong>Error!</strong> '. $showError.' <button type="button" class="close" data-dismiss="alert aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> '; } if($exists) { echo ' <div class="alert alert-danger alert-dismissible fade show" role="alert"> <strong>Error!</strong> '. $exists.' <button type="button" class="close" data-dismiss="alert" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> '; } ?> <div class="container my-4 "> <h1 class="text-center">Signup Here</h1> <form action="signup.php" method="post"> <div class="form-group"> <label for="username">Username</label> <input type="text" class="form-control" id="username" name="username" aria-describedby="emailHelp"> </div> <div class="form-group"> <label for="password">Password</label> <input type="password" class="form-control" id="password" name="password"> </div> <div class="form-group"> <label for="cpassword">Confirm Password</label> <input type="password" class="form-control" id="cpassword" name="cpassword"> <small id="emailHelp" class="form-text text-muted"> Make sure to type the same password </small> </div> <button type="submit" class="btn btn-primary"> SignUp </button> </form> </div> <!-- Optional JavaScript --> <!-- jQuery first, then Popper.js, then Bootstrap JS --> <script src=" https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity=" sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"> </script> <script src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity= "sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"> </script> <script src=" https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity= "sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"> </script> </body> </html> Output: Sign-up form: After successful user login: After invalid user login: Comment More infoAdvertise with us Next Article Online Group Chat application using PHP mynkgpt16 Follow Improve Article Tags : PHP HTML-Misc PHP-Misc Similar Reads How to create admin login page using PHP? In this article, we will see how we can create a login page for admin, connected with the database, or whose information to log in to the page is already stored in our database. Follow the steps to create an admin login page using PHP: Approach: Make sure you have XAMPP or WAMP installed on your w 4 min read Signup form using PHP and MySQL Database The task is to create and design a sign-up form in which if the user enters details, the HTML form data are inserted into our MySQL server database. Approach: First task is that we have to create our MySQL server Database and a Table according to our requirements. We connect our MySQL server Databas 4 min read Online Group Chat application using PHP Prerequisites: Technical knowledge: HTMLCSSJavascript (basics)PHP (Database connectivity)SQL queries Softwares to be installed: XAMPP server: This is a free software which contains web server Apache, Database management system for MySQL (or MariaDB). It can be downloaded for free from the official s 9 min read How to Resize JPEG Image in PHP ? Why do we need to resize images? In a website, often, we need to scale an image to fit a particular section. Sometimes, it becomes necessary to scale down any image of random size to fit a cover photo section or profile picture section. Also, we need to show a thumbnail of a bigger image. In those c 2 min read How to make PDF file downloadable in HTML link using PHP ? In web development, it is common to provide users with downloadable resources, such as PDF files. If you want to create a downloadable PDF link using HTML and PHP, this article will guide you through the process of making a PDF file downloadable when the user clicks on a link. ApproachCreate an HTML 3 min read How to extract img src and alt from html using PHP? Extraction of image attributes like 'src', 'alt', 'height', 'width' etc from a HTML page using PHP. This task can be done using the following steps. Loading HTML content in a variable(DOM variable). Selecting each image in that document. Selecting attribute and save it's content to a variable. Outpu 2 min read Upload pdf file to MySQL database for multiple records using PHP We will upload multiple records to the database and display all the records from the database on the same page. In this article, we will see how we can upload PDF files to a MySQL database using PHP. Approach: Make sure you have XAMPP or WAMP installed on your machine. In this tutorial, we will be 7 min read Mailer multiple address in PHP In this article, we will be demonstrating how we can send the mail to the multiple addresses from the database using the PHP. PHPMailer library is used to send any email safely from the unknown e-mail to any mail id using PHP code through XAMPP web-server for this project. Installation process for a 2 min read Covid 19 Tracker Web App using PHP In this article, we will see how to create a web application for tracking the Covid19 using PHP. Our Covid19 Tracker app will give the latest information for the States and Union Territories of India about the following things.Number of Active Cases of Covid19.Number of Confirmed Cases of Covid19.Nu 3 min read How to automatically start a download in PHP ? This post deals with creating a start downloading file using PHP. The idea is to make a download button which will redirect you to another page with the PHP script that will automatically start the download. Creating a download button: html <!DOCTYPE html> <html> <head> <meta na 2 min read Like