How to Create Registration Page in PHP/MySQL Using PDO Query

This is a simple tutorial that will teach you on how to create a simple registration form using PHP/MySQL using PDO Query and server-side error validation. This tutorial will not teach you on how to create a good design but rather to give you knowledge on how to create a fully functional registration form. This tutorial is different from my previous registration page tutorial. To find out what's the difference, follow the steps bellow

Creating Our Database

First we are going to create our database which stores our data. To create a database: 1. Open phpmyadmin 2. Then create database and name it as "pdo_ret". 3. After creating a database name, click the SQL and paste the following code.
  1. CREATE TABLE IF NOT EXISTS `members` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT,
  3. `fname` varchar(100) NOT NULL,
  4. `lname` varchar(100) NOT NULL,
  5. `age` int(5) NOT NULL,
  6. PRIMARY KEY (`id`)
  7. ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Creating The Form

Next step is to create a form and save it as index.php. The code bellow include the code that display the error validation generated by the server. To create a form, open your HTML code editor and paste the code below after the tag.
  1. <?php
  2. ?>
  3. <?php
  4. if( isset($_SESSION['ERRMSG_ARR']) && is_array($_SESSION['ERRMSG_ARR']) && count($_SESSION['ERRMSG_ARR']) >0 ) {
  5. echo '<ul style="padding:0; color:red;">';
  6. foreach($_SESSION['ERRMSG_ARR'] as $msg) {
  7. echo '<li>',$msg,'</li>';
  8. }
  9. echo '</ul>';
  10. unset($_SESSION['ERRMSG_ARR']);
  11. }
  12. ?>
  13. <form action="reg.php" method="POST">
  14. First Name<br>
  15. <input type="text" name="fname" /><br>
  16. Last Name<br>
  17. <input type="text" name="lname" /><br>
  18. Age<br>
  19. <input type="text" name="age" /><br>
  20. <input type="submit" value="Save" />
  21. </form>

Writing Our Save Script

Next step is to create our script that save our input data to database and save it as "reg.php". The code bellow contains server-side validation and the save scripts.
  1. <?php
  2. $errmsg_arr = array();
  3. $errflag = false;
  4. // configuration
  5. $dbhost = "localhost";
  6. $dbname = "pdo_ret";
  7. $dbuser = "root";
  8. $dbpass = "";
  9.  
  10. // database connection
  11. $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
  12.  
  13. // new data
  14.  
  15. $fname = $_POST['fname'];
  16. $lname = $_POST['lname'];
  17. $age = $_POST['age'];
  18.  
  19. if($fname == '') {
  20. $errmsg_arr[] = 'You must enter your First Name';
  21. $errflag = true;
  22. }
  23. if($lname == '') {
  24. $errmsg_arr[] = 'You must enter your Last Name';
  25. $errflag = true;
  26. }
  27. if($age == '') {
  28. $errmsg_arr[] = 'You must enter your Age';
  29. $errflag = true;
  30. }
  31. if($errflag) {
  32. $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  33. header("location: index.php");
  34. exit();
  35. }
  36. // query
  37. $sql = "INSERT INTO members (fname,lname,age) VALUES (:sas,:asas,:asafs)";
  38. $q = $conn->prepare($sql);
  39. $q->execute(array(':sas'=>$fname,':asas'=>$lname,':asafs'=>$age));
  40. header("location: index.php");
  41.  
  42.  
  43. ?>
That's it! You've been successfully created your simple registration form with PDO Query and server-side validation.

Comments

Submitted byseniorson (not verified)on Fri, 06/12/2015 - 14:48

The error i keep getting is PHP Fatal error: Class 'config' not found in G:\PleskVhosts\wajane254.com\httpdocs\reg.php on line 12 That is when it is loading the reg.php set('dbhost', 'localhost'); //probably localhost $config->set('dbname', 'pdo_ret'); $config->set('dbuser', 'oscar'); $config->set('dbpass', '(oscar*oscar)'); // database connection $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); // new data $gname = $_POST['nameofgroup']; $fname = $_POST['firstname']; $sname = $_POST['secondname']; $lname = $_POST['lastname']; $age = $_POST['age']; $residence = $_POST['residence']; $pbirth = $_POST['placeofbirth']; $yowidowhood = $_POST['yrowidowhood']; $nodependants = $_POST['nodependant']; $edulevel = $_POST['edulevel']; $contact = $_POST['contact']; $nexkinconts = $_POST['nexkinconts']; if($gname == '') { $errmsg_arr[] = 'You must enter your Group Name'; $errflag = true; } if($fname == '') { $errmsg_arr[] = 'You must enter your First Name'; $errflag = true; } if($sname == '') { $errmsg_arr[] = 'You must enter your Second Name'; $errflag = true; } if($lname == '') { $errmsg_arr[] = 'You must enter your Last Name'; $errflag = true; } if($age == '') { $errmsg_arr[] = 'You must enter your Age'; $errflag = true; } if($residence == '') { $errmsg_arr[] = 'You must enter your Residence'; $errflag = true; } if($pbirth == '') { $errmsg_arr[] = 'You must enter your Place of Birth'; $errflag = true; } if($yowidowhood == '') { $errmsg_arr[] = 'You must enter your Year of Widowhood'; $errflag = true; } if($nodependants == '') { $errmsg_arr[] = 'You must enter your number of dependants'; $errflag = true; } if($edulevel == '') { $errmsg_arr[] = 'You must enter your Education level'; $errflag = true; } if($contact == '') { $errmsg_arr[] = 'You must enter your Phone Contact'; $errflag = true; } if($nextkincont == '') { $errmsg_arr[] = 'You must enter your Next of kin Contact'; $errflag = true; } if($errflag) { $_SESSION['ERRMSG_ARR'] = $errmsg_arr; session_write_close(); header("location: index.php"); exit(); } // query $sql = "INSERT INTO member (nameofgroup,firstname,secondname,lastname,age,residence,placeofbirth,yrowidowhood,nodependants,edulevel,contact,nextkincont) VALUES (:a,:b,:c,:d,:e,:f,:g,:h,:i,:j,:k,:l)"; $q = $conn->prepare($sql); $q->execute(array(':a'=>$nameofgroup,':b'=>$firstname,':c'=>$secondname,':d'=>$lastname,':e'=>$age,':f'=>$residence,':g'=>$placeofbirth,':h'=>$yrowidowhood,':i'=>$nodependants,':j'=>$edulevel,':k'=>$contact,':l'=>$nexkincontact)); header("location: success.php"); ?>
Submitted byGreats (not verified)on Fri, 12/04/2015 - 05:21

I did download the code that you have for downloading on this page. When I open the Index.php in firefox then i can't see the red text that should be above the login. I get this this text insted. 0 ) { echo ' '; foreach($_SESSION['ERRMSG_ARR'] as $msg) { echo ' ',$msg,' '; } echo ' '; unset($_SESSION['ERRMSG_ARR']); } ?> Any chance you can help me.?

you need a local server installed XAMPP OR WAMP and ensure you stop the source file u downloaded in c/xampp/htdocs/(downloaded folder).Ensure xampp is running and u r on.

Add new comment