Registration Form Using PDO in PHP


Today I will explain how to create a Registration Form Using PDO in PHP. Basically, PDO stands for PHP Data Objects and allows us to access data from a data source.

Creating Registration Form Using PDO in PHP

The following section describes how to create an application using PHP and HTML that allows users to register for an event. At first, we create a form in HTML containing certain fields that we want users to fill for registration.

Registration Form in HTML

Since we want to use the data entered in form fields, we need to provide the name attribute for each text field. Later, we can use the $_POST variables of PHP to retrieve the value of the corresponding text field.


<!DOCTYPE html>
    <meta charset="UTF-8">
    <title> Event Registration Form</title>
    <link rel="stylesheet" href="">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <div class="container">
    <div class="title">Event Registration</div>
    <div class="content">
      <form action="registered.php" method="POST">
        <div class="dx">
          <div class="b2x">
            <span class="details">Enrolment No.</span>
            <input type="text" placeholder="Enter your enrolment no." name="enroll" required>
          <div class="b2x">
            <span class="details">Full Name</span>
            <input type="text" placeholder="Enter your name" name="fname" required>
          <div class="b2x">
            <span class="details">Email</span>
            <input type="text" placeholder="Enter your email" name="email" required>
          <div class="b2x">
            <span class="details">Contact Number</span>
            <input type="text" placeholder="Enter your number" name="contact" required>
          <div class="b2x">
            <span class="details">Course</span>
            <input type="text" placeholder="Enter your course" name="course" required>
          <div class="b2x">
            <span class="details">Semester</span>
            <input type="text" placeholder="Enter semester" name="semester" required>
          <div class="b2x">
            <span class="details">Shift (M or E)</span>
            <input type="text" placeholder="Enter Shift" name="shift" required>
        <div class="button">
          <input type="submit" value="Register">


The following image displays the form.

Creating Registration Form Using PDO in PHP
Creating Registration Form Using PDO in PHP

Creating Connection with MySQL Database

Once, the form is created, we need a database table to store the form data. Hence, we create a table in a MySQL database as shown below.

Table Structure for Storing Registration Details
Table Structure for Storing Registration Details

In order to connect the application with the database, a PDO object is required that requires hostname, database name, username, and password as parameters.


   // define database related variables
	$servername = "localhost";
	$username = "your username";
	$password = "your password";
	$dbname = "database name";
   // try to conncet to database
		$con1 = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
catch(PDOException $e)
		echo "Connection failed: " . $e->getMessage();

Now that, we have established a connection with the database, we need to run the insert command of SQL using PDO. At firt, we start the session, set default time zone, and include the db.php file containing a PDO object.

After that, we retrieve the form data using $_POST array. Once, we have the form values with us, it is desired to create session variables so that we can use this data across other web pages. However, in this example, session variables are not being used.

In order to run the inser command, first we create a string with the command text. After that, we call the prepare() method so that we get a PDOStatement object. The bindValue() function binds the specified value to a given variable name specified in the insert statement. Finally, the execute() method executes the insert statement.




$dt=strftime("%B-%d-%Y %H:%M:%S", $currenttime);


$sql="insert into dea(enrol, pname, email, contactno, course, sem, shift, regdate) values(:ct, :sn, :em, :cn, :pg, :cr, :qr, :qd)";
$stmt->bindValue(':sn', $fname);
$stmt->bindValue(':em', $email);
$stmt->bindValue(':cn', $contact);
$stmt->bindValue(':ct', $enroll);
$stmt->bindValue(':pg', $course);
$stmt->bindValue(':cr', $semester);
$stmt->bindValue(':qr', $sh);
$stmt->bindValue(':qd', $dt);

    $res= 'Thank You!';
     $res= 'Temporary Error! Please try after some time.';



Inserting a Record using PDO
Inserting a Record using PDO

Further Reading

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

PHP Projects for Undergraduate Students

Architectural Constraints of REST API

REST API Concepts

Creating a Classified Ads Application in PHP


You may also like...