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

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

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.  
  5. CREATE TABLE `user_registration` (
  6. `user_id` INT(11) NOT NULL,
  7. `first_name` VARCHAR(100) NOT NULL,
  8. `last_name` VARCHAR(100) NOT NULL,
  9. `user_name` VARCHAR(100) NOT NULL,
  10. `password` VARCHAR(100) NOT NULL
  11. ) 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.  
  3. <h2>Registration Form</h2>
  4. <table border="0" cellspacing="3" cellpadding="3" width="80%">
  5. <tr>
  6. <td><label>First Name</label></td>
  7. <td><input type="text" name="first_name" autofocus="autofocus"></td>
  8. </tr>
  9. <tr>
  10. <td><label>Last Name</label></td>
  11. <td><input type="text" name="last_name" autofocus="autofocus"></td>
  12. </tr>
  13. <tr>
  14. <td><label>User Name</label></td>
  15. <td><input type="text" name="user_name" autofocus="autofocus"></td>
  16. </tr>
  17. <tr>
  18. <td><label>Password</label></td>
  19. <td><input type="password" name="password" autofocus="autofocus"></td>
  20. </tr>
  21. <tr>
  22. <td colspan="2">
  23. <button class="submit_btn" type="submit" name="submit">Register</button>
  24. <button class="cancel_btn" type="reset" name="cancel">Cancel</button>
  25. </td>
  26. </tr>
  27.  
  28. </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.  
  7. $conn = new PDO("mysql:host=$db_server;dbname=$db_database", $db_username, $db_password);
  8. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  9. ?>

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.  
  7. $first_name=$_POST['first_name'];
  8. $last_name=$_POST['last_name'];
  9. $user_name=$_POST['user_name'];
  10. $password1=$_POST['password'];
  11.  
  12. $conn = new PDO("mysql:host=$db_server;dbname=$db_database", $db_username, $db_password);
  13.  
  14. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  15. $sql = "INSERT INTO user_registration (first_name, last_name, user_name, password)
  16. VALUES ('$first_name', '$last_name', '$user_name', '$password1')";
  17.  
  18. $conn->exec($sql);
  19. echo "<script>alert('Account successfully added!'); window.location='index.php'</script>";
  20. ?>

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.

Form FieldThis is the result of registering the account and saving into database.

Message Alert

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.


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.