• We just launched and are currently in beta. Join us as we build and grow the community.

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

angelizmir

Rank Recovery Specialist
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
95
Likes
154
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
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.png

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. if

    (

    mysqli_connect_errno

    (

    )

    )
  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. <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. <?php
  11. // define variables and set to empty values
  12. $Message

    =

    $ErrorUname

    =

    $ErrorPass

    =

    $ErrorEmail

    =

    $ErrorName

    =

    ""

    ;
  13. $username

    =

    $password

    =

    $email

    =

    $fullname

    =

    ""

    ;

  14. if

    (

    $_SERVER

    [

    "REQUEST_METHOD"

    ]

    ==

    "POST"

    )

    {
  15. if

    (

    empty

    (

    $_POST

    [

    "username"

    ]

    )

    )

    {
  16. $ErrorUname

    =

    "Userame is required"

    ;
  17. }

    else

    {
  18. $username

    =

    check_input(

    $_POST

    [

    "username"

    ]

    )

    ;
  19. // check if name only contains letters and whitespace
  20. if

    (

    !

    preg_match

    (

    "/^[a-zA-Z0-9_]*$/"

    ,

    $username

    )

    )

    {
  21. $ErrorUname

    =

    "Space and special characters not allowed but you can use underscore(_)."

    ;
  22. }
  23. else

    {
  24. $fusername

    =

    $username

    ;
  25. }
  26. }

  27. if

    (

    empty

    (

    $_POST

    [

    "password"

    ]

    )

    )

    {
  28. $ErrorPass

    =

    "Password is required"

    ;
  29. }

    else

    {
  30. $fpassword

    =

    check_input(

    $_POST

    [

    "password"

    ]

    )

    ;
  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. if

    (

    empty

    (

    $_POST

    [

    "fullname"

    ]

    )

    )

    {
  45. $ErrorName

    =

    "Full name is required"

    ;
  46. }

    else

    {
  47. $fullname

    =

    check_input(

    $_POST

    [

    "fullname"

    ]

    )

    ;
  48. // check if name only contains letters and whitespace
  49. if

    (

    !

    preg_match

    (

    "/^[a-zA-Z ]*$/"

    ,

    $fullname

    )

    )

    {
  50. $ErrorName

    =

    "Only letters and white space allowed"

    ;
  51. }
  52. else

    {
  53. $ffullname

    =

    $fullname

    ;
  54. }
  55. }

  56. if

    (

    $ErrorUname

    !=

    ""

    OR $ErrorPass

    !=

    ""

    OR $ErrorEmail

    !=

    ""

    OR $ErrorName

    !=

    ""

    )

    {
  57. $Message

    =

    "Registration failed! Errors found"

    ;
  58. }
  59. else

    {
  60. include

    (

    'conn.php'

    )

    ;
  61. mysqli_query

    (

    $conn

    ,

    "insert into `user` (username,password,email_add,fullname) values ('$fusername

    ','$fpassword

    ','$femail

    ','$ffullname

    ')"

    )

    ;
  62. $Message

    =

    "Registration Successful!"

    ;
  63. }
  64. }

  65. function

    check_input(

    $data

    )

    {
  66. $data

    =

    trim

    (

    $data

    )

    ;
  67. $data

    =

    stripslashes

    (

    $data

    )

    ;
  68. $data

    =

    htmlspecialchars

    (

    $data

    )

    ;
  69. return

    $data

    ;
  70. }
  71. ?>

  72. <h2>Sign Up Form</h2>
  73. <p><span class="error">* required field.</span></p>
  74. <form method="post" action="<?php

    echo

    htmlspecialchars

    (

    $_SERVER

    [

    "PHP_SELF"

    ]

    )

    ;

    ?>

    ">
  75. Username: <input type="text" name="username" value="<?php

    echo

    isset

    (

    $_POST

    [

    'username'

    ]

    )

    ? $_POST

    [

    'username'

    ]

    :

    ""

    ?>

    ">
  76. <span class="error">* <?php

    echo

    $ErrorUname

    ;

    ?>

    </span>
  77. <br><br>
  78. Password: <input type="password" name="password" value="<?php

    echo

    isset

    (

    $_POST

    [

    'password'

    ]

    )

    ? $_POST

    [

    'password'

    ]

    :

    ""

    ?>

    ">
  79. <span class="error">* <?php

    echo

    $ErrorPass

    ;

    ?>

    </span>
  80. <br><br>
  81. Email: <input type="text" name="email" value="<?php

    echo

    isset

    (

    $_POST

    [

    'email'

    ]

    )

    ? $_POST

    [

    'email'

    ]

    :

    ""

    ?>

    ">
  82. <span class="error">* <?php

    echo

    $ErrorEmail

    ;

    ?>

    </span>
  83. <br><br>
  84. Name: <input type="text" name="fullname" value="<?php

    echo

    isset

    (

    $_POST

    [

    'fullname'

    ]

    )

    ? $_POST

    [

    'fullname'

    ]

    :

    ""

    ?>

    ">
  85. <span class="error">* <?php

    echo

    $ErrorName

    ;

    ?>

    </span>
  86. <br><br>
  87. <input type="submit" name="submit" value="Submit">
  88. <br><br>
  89. <span class="error"><?php

    echo

    $Message

    ;

    ?>

    </span>

  90. </form>

  91. </body>
  92. </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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

333,651

333,659

Top