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

How To Create Registration Form In PHP/MySQL Using PDO Query

udbryte

Immersion Expert
U Rep
0
0
0
Rep
0
U Vouches
0
0
0
Vouches
0
Posts
122
Likes
186
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Related Code: Registration Page In PHP/MySQL

If you are looking for on How To Create Registration Form In PHP/MySQL Using PDO Query then you are at the right place. In this tutorial, we are going to learn on how to create registration page using PHP/MySQL but with the use of PDO query.

In this article, the user types their information in the form field to save in the database. After saving the data, the alert message will pop up and said: "Account successfully added!".

Creating our Table

We are going to make our database.

To create a database:

  1. Open the PHPMyAdmin.
  2. Create a database and name it as "registration_pdo".
  3. After creating a database name, then we are going to create our table. And name it as "user_registration".

Kindly copy the code below.

  1. --
  2. -- Table structure for table `user_registration`
  3. --

  4. CREATE

    TABLE

    `user_registration`

    (
  5. `user_id`

    INT

    (

    11

    )

    NOT

    NULL

    ,
  6. `first_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  7. `last_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  8. `user_name`

    VARCHAR

    (

    100

    )

    NOT

    NULL

    ,
  9. `password`

    VARCHAR

    (

    100

    )

    NOT

    NULL
  10. )

    ENGINE=

    InnoDB DEFAULT

    CHARSET=

    latin1;

Second, we are going to make our form field.
Creating Form Field

This form field that the user types their information to save in the database.

  1. <form

    method

    =

    "post"

    action

    =

    "insert_query.php"

    >

  2. <h2

    >

    Registration Form</

    h2

    >
  3. <table

    border

    =

    "0"

    cellspacing

    =

    "3"

    cellpadding

    =

    "3"

    width

    =

    "80%"

    >
  4. <tr

    >
  5. <td

    ><label

    >

    First Name</

    label

    ></

    td

    >
  6. <td

    ><input

    type

    =

    "text"

    name

    =

    "first_name"

    autofocus=

    "autofocus"

    ></

    td

    >
  7. </

    tr

    >
  8. <tr

    >
  9. <td

    ><label

    >

    Last Name</

    label

    ></

    td

    >
  10. <td

    ><input

    type

    =

    "text"

    name

    =

    "last_name"

    autofocus=

    "autofocus"

    ></

    td

    >
  11. </

    tr

    >
  12. <tr

    >
  13. <td

    ><label

    >

    User Name</

    label

    ></

    td

    >
  14. <td

    ><input

    type

    =

    "text"

    name

    =

    "user_name"

    autofocus=

    "autofocus"

    ></

    td

    >
  15. </

    tr

    >
  16. <tr

    >
  17. <td

    ><label

    >

    Password</

    label

    ></

    td

    >
  18. <td

    ><input

    type

    =

    "password"

    name

    =

    "password"

    autofocus=

    "autofocus"

    ></

    td

    >
  19. </

    tr

    >
  20. <tr

    >
  21. <td

    colspan

    =

    "2"

    >
  22. <button

    class

    =

    "submit_btn"

    type

    =

    "submit"

    name

    =

    "submit"

    >

    Register</

    button

    >
  23. <button

    class

    =

    "cancel_btn"

    type

    =

    "reset"

    name

    =

    "cancel"

    >

    Cancel</

    button

    >
  24. </

    td

    >
  25. </

    tr

    >
  26. </

    table

    >

  27. </

    form

    >

Third, we are going to make our database connection.
Database Connection

This PHP Script is our database. Copy and paste this then save it as "connection.php".

  1. <?php
  2. $db_server

    =

    "localhost"

    ;
  3. $db_username

    =

    "root"

    ;
  4. $db_password

    =

    ""

    ;
  5. $db_database

    =

    "registration_pdo"

    ;

  6. $conn

    =

    new

    PDO(

    "mysql:host=$db_server

    ;dbname=$db_database

    "

    ,

    $db_username

    ,

    $db_password

    )

    ;
  7. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  8. ?>

Fourth, we are going to make our saving PHP Script.
Saving Script - PHP

This step is to create our saving script for our data to save in the database and save it as "insert_query.php".

  1. <?php
  2. $db_server

    =

    "localhost"

    ;
  3. $db_username

    =

    "root"

    ;
  4. $db_password

    =

    ""

    ;
  5. $db_database

    =

    "registration_pdo"

    ;

  6. $first_name

    =

    $_POST

    [

    'first_name'

    ]

    ;
  7. $last_name

    =

    $_POST

    [

    'last_name'

    ]

    ;
  8. $user_name

    =

    $_POST

    [

    'user_name'

    ]

    ;
  9. $password1

    =

    $_POST

    [

    'password'

    ]

    ;

  10. $conn

    =

    new

    PDO(

    "mysql:host=$db_server

    ;dbname=$db_database

    "

    ,

    $db_username

    ,

    $db_password

    )

    ;

  11. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  12. $sql

    =

    "INSERT INTO user_registration (first_name, last_name, user_name, password)
  13. VALUES ('$first_name

    ', '$last_name

    ', '$user_name

    ', '$password1

    ')"

    ;

  14. $conn

    ->

    exec

    (

    $sql

    )

    ;
  15. echo

    "<script>alert('Account successfully added!'); window.location='index.php'</script>"

    ;
  16. ?>

And, this is our style for the layout of the Form Field.
  1. <style type=

    "text/css"

    >
  2. body {
  3. width

    :

    600px

    ;
  4. border

    :

    red

    1px

    solid

    ;
  5. border-style

    :

    dashed

    ;
  6. margin

    :

    auto

    ;
  7. padding

    :

    10px

    ;
  8. }
  9. td {
  10. text-align

    :

    center

    ;
  11. padding

    :

    10px

    ;
  12. }
  13. table

    {
  14. margin

    :

    auto

    ;
  15. }
  16. input[

    type=

    "text"

    ]

    {
  17. width

    :

    150px

    ;
  18. font-size

    :

    18px

    ;
  19. border

    :

    blue

    1px

    solid

    ;
  20. text-indent

    :

    5px

    ;
  21. cursor

    :

    pointer

    ;
  22. }
  23. input[

    type=

    "password"

    ]

    {
  24. width

    :

    150px

    ;
  25. font-size

    :

    18px

    ;
  26. border

    :

    blue

    1px

    solid

    ;
  27. text-indent

    :

    5px

    ;
  28. cursor

    :

    pointer

    ;
  29. }
  30. label {
  31. font-size

    :

    18px

    ;
  32. color

    :

    blue

    ;
  33. font-weight

    :

    bold

    ;
  34. font-family

    :

    cursive

    ;
  35. }
  36. h2 {
  37. color

    :

    red

    ;
  38. text-align

    :

    center

    ;
  39. }
  40. .submit_btn

    {
  41. margin-right

    :

    70px

    ;
  42. border

    :

    blue

    1px

    solid

    ;
  43. font-size

    :

    18px

    ;
  44. font-family

    :

    cursive

    ;
  45. padding

    :

    5px

    ;
  46. font-weight

    :

    bold

    ;
  47. color

    :

    blue

    ;
  48. background

    :

    azure

    ;
  49. border-radius

    :

    4px

    ;
  50. }
  51. .submit_btn

    :

    hover

    {
  52. margin-right

    :

    70px

    ;
  53. border

    :

    red

    1px

    solid

    ;
  54. font-size

    :

    18px

    ;
  55. font-family

    :

    cursive

    ;
  56. padding

    :

    5px

    ;
  57. font-weight

    :

    bold

    ;
  58. color

    :

    white

    ;
  59. background

    :

    blue

    ;
  60. border-radius

    :

    4px

    ;
  61. cursor

    :

    pointer

    ;
  62. }
  63. .cancel_btn

    {
  64. border

    :

    red

    1px

    solid

    ;
  65. font-size

    :

    18px

    ;
  66. font-family

    :

    cursive

    ;
  67. padding

    :

    5px

    ;
  68. font-weight

    :

    bold

    ;
  69. color

    :

    red

    ;
  70. background

    :

    azure

    ;
  71. border-radius

    :

    4px

    ;
  72. }
  73. .cancel_btn

    :

    hover

    {
  74. border

    :

    blue

    1px

    solid

    ;
  75. font-size

    :

    18px

    ;
  76. font-family

    :

    cursive

    ;
  77. padding

    :

    5px

    ;
  78. font-weight

    :

    bold

    ;
  79. color

    :

    white

    ;
  80. background

    :

    red

    ;
  81. border-radius

    :

    4px

    ;
  82. cursor

    :

    pointer

    ;
  83. }
  84. </style>

Output:

This is our Registration Form. For the new user, type their information to save into database.
1_56.png

This is the result of registering the account and saving into database.
2_24.png


Related Code: Registration Page In PHP/MySQL

So, this is it, just follow the steps to have this Registration Page or you can download the full source code below by clicking the "Download Code" button below.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.


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

452,292

323,341

323,350

Top