Skip to main content

Creating a Simple Sign Up Form with Validation in PHP/MySQLi Tutorial

Language

This tutorial will show you how to create a simple sign-up form with validation using PHP/MySQLi. This tutorial does not include a good design but will give you an idea of how to create a simple Sign Up form using PHP/MySQLi. The sign-up form that we will create has a validation that returns messages in each field that has a value that hasn't reach the input field requirement such as null values and email type.

Before we start, please download and install XAMPP first or any equivalent to run PHP Script in you local machine. If you are using XAMPP/WAMP open the software Control Panel and start Apache and MySQL.

Let's Get Started ...

Creating our Database

First, we're going to create a database that will store our data.

  1. Open phpMyAdmin.
  2. Click databases, create a database and name it as sign_up.
  3. After creating a database, click the SQL and paste the below code. See the image below for detailed instructions.
  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(30) NOT NULL,
  4. `password` VARCHAR(30) NOT NULL,
  5. `email_add` VARCHAR(100) NOT NULL,
  6. `fullname` VARCHAR(100) NOT NULL,
  7. PRIMARY KEY (`userid`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
steps_signup

Creating our Connection

Next, we create a database connection and save it as conn.php. This file will serve as the bridge between our form and our database.

  1. <?php
  2. $conn = mysqli_connect("localhost","root","","sign_up");
  3. // Check connection
  4. {
  5. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  6. }
  7. ?>

Creating our Form and Save Sign Up Script

Lastly, we create our sign-up form with the save script and save it as index.php. In this form, the inputted data will be saved upon user submission if there are no errors in the input. To create the form, open your HTML code editor and paste the code below after the tag.

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Register and Login Form with Validation PHP, MySQLi</title>
  5. <style>
  6. .error {color: #FF0000;}
  7. </style>
  8. </head>
  9. <body>
  10.  
  11. <?php
  12. // define variables and set to empty values
  13. $Message = $ErrorUname = $ErrorPass = $ErrorEmail = $ErrorName = "";
  14. $username = $password = $email = $fullname = "";
  15.  
  16. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  17. if (empty($_POST["username"])) {
  18. $ErrorUname = "Userame is required";
  19. } else {
  20. $username = check_input($_POST["username"]);
  21. // check if name only contains letters and whitespace
  22. if (!preg_match("/^[a-zA-Z0-9_]*$/",$username)) {
  23. $ErrorUname = "Space and special characters not allowed but you can use underscore(_).";
  24. }
  25. else{
  26. $fusername=$username;
  27. }
  28. }
  29.  
  30. if (empty($_POST["password"])) {
  31. $ErrorPass = "Password is required";
  32. } else {
  33. $fpassword = check_input($_POST["password"]);
  34. }
  35.  
  36. if (empty($_POST["email"])) {
  37. $ErrorEmail = "Email is required";
  38. } else {
  39. $email = check_input($_POST["email"]);
  40. // check if e-mail address is well-formed
  41. if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  42. $ErrorEmail = "Invalid email format";
  43. }
  44. else{
  45. $femail=$email;
  46. }
  47. }
  48.  
  49. if (empty($_POST["fullname"])) {
  50. $ErrorName = "Full name is required";
  51. } else {
  52. $fullname = check_input($_POST["fullname"]);
  53. // check if name only contains letters and whitespace
  54. if (!preg_match("/^[a-zA-Z ]*$/",$fullname)) {
  55. $ErrorName = "Only letters and white space allowed";
  56. }
  57. else{
  58. $ffullname=$fullname;
  59. }
  60. }
  61.  
  62. if ($ErrorUname!="" OR $ErrorPass!="" OR $ErrorEmail!="" OR $ErrorName!=""){
  63. $Message = "Registration failed! Errors found";
  64. }
  65. else{
  66. include('conn.php');
  67. mysqli_query($conn,"insert into `user` (username,password,email_add,fullname) values ('$fusername','$fpassword','$femail','$ffullname')");
  68. $Message = "Registration Successful!";
  69. }
  70. }
  71.  
  72. function check_input($data) {
  73. $data = trim($data);
  74. $data = stripslashes($data);
  75. $data = htmlspecialchars($data);
  76. return $data;
  77. }
  78. ?>
  79.  
  80. <h2>Sign Up Form</h2>
  81. <p><span class="error">* required field.</span></p>
  82. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  83. Username: <input type="text" name="username" value="<?php echo isset($_POST['username']) ? $_POST['username'] : "" ?>">
  84. <span class="error">* <?php echo $ErrorUname;?></span>
  85. <br><br>
  86. Password: <input type="password" name="password" value="<?php echo isset($_POST['password']) ? $_POST['password'] : "" ?>">
  87. <span class="error">* <?php echo $ErrorPass;?></span>
  88. <br><br>
  89. Email: <input type="text" name="email" value="<?php echo isset($_POST['email']) ? $_POST['email'] : "" ?>">
  90. <span class="error">* <?php echo $ErrorEmail;?></span>
  91. <br><br>
  92. Name: <input type="text" name="fullname" value="<?php echo isset($_POST['fullname']) ? $_POST['fullname'] : "" ?>">
  93. <span class="error">* <?php echo $ErrorName;?></span>
  94. <br><br>
  95. <input type="submit" name="submit" value="Submit">
  96. <br><br>
  97. <span class="error"><?php echo $Message;?></span>
  98.  
  99. </form>
  100.  
  101. </body>
  102. </html>

DEMO

There you go. You can now test the source code. I hope this Simple Sign Up Form with Validation in PHP/MySQLi Tutorial will help you with what you are looking for and also for your future PHP Projects.

Happy Coding :))

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment