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

MD5 Registration using PDO in PHP

user010

Token Flipper
U Rep
0
0
0
Rep
0
U Vouches
0
0
0
Vouches
0
Posts
82
Likes
85
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
In this tutorial, we are going to learn on how to create MD5 Registration using PDO in PHP. This simple project helps you to create registration with an MD5 function to encrypt the password field. The feature of this simple project is to save all the data from the field then the password will be encrypted it’s because of the MD5 function that we set in the PDO query.

We construct simple registration form.

  1. <div

    style

    =

    "width:50%;"

    >
  2. <form

    method

    =

    "POST"

    action

    =

    "insert-query.php"

    >
  3. <div

    >
  4. <label

    >

    First Name</

    label

    >
  5. <input

    type

    =

    "text"

    name

    =

    "first_name"

    placeholder=

    "First Name . . . . ."

    required>
  6. </

    div

    >
  7. <div

    >
  8. <label

    >

    Last Name</

    label

    >
  9. <input

    type

    =

    "text"

    name

    =

    "last_name"

    placeholder=

    "Last Name . . . . ."

    >
  10. </

    div

    >
  11. <div

    >
  12. <label

    >

    Email Address</

    label

    >
  13. <input

    type

    =

    "email"

    name

    =

    "email"

    placeholder=

    "Email Address . . . . ."

    required>
  14. </

    div

    >
  15. <div

    >
  16. <label

    >

    Password</

    label

    >
  17. <input

    type

    =

    "password"

    name

    =

    "password"

    placeholder=

    "Password . . . . ."

    required>
  18. </

    div

    >
  19. <div

    >
  20. <label

    >

    Re-type Password</

    label

    >
  21. <input

    type

    =

    "password"

    name

    =

    "re_password"

    placeholder=

    "Re-type Password . . . . ."

    required>
  22. </

    div

    >
  23. <button

    type

    =

    "submit"

    name

    =

    "btn_submit"

    >

    Submit</

    button

    >
  24. </

    form

    >
  25. </

    div

    >

And, we have to create the query to insert data into the database using PDO in PHP. Copy and paste to your editor and save it as "insert-query.php".

Those variables below, used this to get the information from the form field given by the user to register a new record of data. As you can see, it has MD5 code in the password and re-type password. The purpose of this is to encrypt the password, so that, in the database the password would be encrypted.

  1. <?php
  2. $first_name

    =

    $_POST

    [

    'first_name'

    ]

    ;
  3. $last_name

    =

    $_POST

    [

    'last_name'

    ]

    ;
  4. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  5. $password

    =

    md5

    (

    $_POST

    [

    'password'

    ]

    )

    ;
  6. $re_password

    =

    md5

    (

    $_POST

    [

    're_password'

    ]

    )

    ;
  7. ?>

We create a simple trapper, like, we have password and re-type password. If the user type their desired password but the re-type password field does not match in the password field, this simple trapper will give an alert message to the user that their information is incorrect.

  1. <?php

  2. if

    (

    $password

    !=

    $re_password

    )
  3. {
  4. echo

    "<script>alert('Your Password does not match . . .'); window.location = 'index.php' </script>"

    ;
  5. }
  6. ?>

PHP query below, this source code used to insert new record of data in the database. Using INSERT Statement. After that, if the data was inserted to the database, the query give an alert message to the user that their data inserted was successful.

  1. <?php
  2. $database

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  3. $insert_query

    =

    "INSERT INTO tbl_user (first_name, last_name, email, password, re_password)
  4. VALUES (?, ?, ?, ?, ?)"

    ;
  5. $insert

    =

    $database

    ->

    prepare

    (

    $insert_query

    )

    ;
  6. $insert

    ->

    execute

    (

    array

    (
  7. $first_name

    ,
  8. $last_name

    ,
  9. $email

    ,
  10. $password

    ,
  11. $re_password
  12. )

    )

    ;
  13. echo

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

    ;
  14. ?>

This is the full source code for PHP query.

  1. <?php
  2. require_once

    (

    'database.php'

    )

    ;

  3. if

    (

    isset

    (

    $_POST

    [

    'btn_submit'

    ]

    )

    )
  4. {
  5. $first_name

    =

    $_POST

    [

    'first_name'

    ]

    ;
  6. $last_name

    =

    $_POST

    [

    'last_name'

    ]

    ;
  7. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  8. $password

    =

    md5

    (

    $_POST

    [

    'password'

    ]

    )

    ;
  9. $re_password

    =

    md5

    (

    $_POST

    [

    're_password'

    ]

    )

    ;
  10. if

    (

    $password

    !=

    $re_password

    )
  11. {
  12. echo

    "<script>alert('Your Password does not match . . .'); window.location = 'index.php' </script>"

    ;
  13. }
  14. else
  15. {
  16. $database

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  17. $insert_query

    =

    "INSERT INTO tbl_user (first_name, last_name, email, password, re_password)
  18. VALUES (?, ?, ?, ?, ?)"

    ;
  19. $insert

    =

    $database

    ->

    prepare

    (

    $insert_query

    )

    ;
  20. $insert

    ->

    execute

    (

    array

    (
  21. $first_name

    ,
  22. $last_name

    ,
  23. $email

    ,
  24. $password

    ,
  25. $re_password
  26. )

    )

    ;
  27. echo

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

    ;
  28. }
  29. }

  30. ?>

Database Name: db_md5

Here's the output.

md5-reg-1.jpg


This tutorial suit for the beginners who really want to know how to construct PHP query using PDO and to insert data into the database. If you have a question in this tutorial, don't hesitate to leave a comment below. Thank you.


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

452,292

323,526

323,535

Top