How to Create Registration Page in PHP/MySQL Using PDO Query
Submitted by argie on Friday, November 22, 2013 - 08:12.
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
That's it! You've been successfully created your simple registration form with PDO Query and server-side validation.
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.- CREATE TABLE IF NOT EXISTS `members` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `fname` varchar(100) NOT NULL,
- `lname` varchar(100) NOT NULL,
- `age` int(5) NOT NULL,
- ) 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.- <?php
- ?>
- <?php
- echo '<ul style="padding:0; color:red;">';
- foreach($_SESSION['ERRMSG_ARR'] as $msg) {
- echo '<li>',$msg,'</li>';
- }
- echo '</ul>';
- }
- ?>
- <form action="reg.php" method="POST">
- First Name<br>
- <input type="text" name="fname" /><br>
- Last Name<br>
- <input type="text" name="lname" /><br>
- Age<br>
- <input type="text" name="age" /><br>
- <input type="submit" value="Save" />
- </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.- <?php
- $errflag = false;
- // configuration
- $dbhost = "localhost";
- $dbname = "pdo_ret";
- $dbuser = "root";
- $dbpass = "";
- // database connection
- $conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
- // new data
- $fname = $_POST['fname'];
- $lname = $_POST['lname'];
- $age = $_POST['age'];
- if($fname == '') {
- $errmsg_arr[] = 'You must enter your First 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($errflag) {
- $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
- }
- // query
- $sql = "INSERT INTO members (fname,lname,age) VALUES (:sas,:asas,:asafs)";
- $q = $conn->prepare($sql);
- ?>
Comments
How to Create Registration Page in PHP/MySQL Using PDO Query
Good job with this code, Argie!
If anyone would like to hide their database info, simply create a config file like this:
getMessage();
}
?>
Then, replace the first 12 lines in the reg.php file above the "// new data" (line 14) line with the following:
Mine is not connecting to the host
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");
?>
Can't see the red text above login.
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.?
Re: redtext above login
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
- Add new comment
- 312 views