Simple Sign Up Form with Validation PHP/MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 3282 times.

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 on how to create a simple Sign Up form using PHP/MySQLi.

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 image below for detailed instruction.

  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 our bridge between our form and our database.

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

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

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.