PHP

Registration Form Using PDO in PHP

Programmingempire

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.

index.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title> Event Registration Form</title>
    <link rel="stylesheet" href="https://api.follow.it/trackstatistics/v2/r8c35hlDQJJFEOYwgvWbpDc0o-FaevbPiRpJj4PXLEcwWGBx2LgUWKL6vP81UURywDzAx0b3GD1Un2ZAiKmatw?key=AJm0xN_JWLqkfghGnLcc2g">
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
   </head>
<body>
  <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>
          <div class="b2x">
            <span class="details">Full Name</span>
            <input type="text" placeholder="Enter your name" name="fname" required>
          </div>
          <div class="b2x">
            <span class="details">Email</span>
            <input type="text" placeholder="Enter your email" name="email" required>
          </div>
          <div class="b2x">
            <span class="details">Contact Number</span>
            <input type="text" placeholder="Enter your number" name="contact" required>
          </div>
          <div class="b2x">
            <span class="details">Course</span>
            <input type="text" placeholder="Enter your course" name="course" required>
          </div>
          <div class="b2x">
            <span class="details">Semester</span>
            <input type="text" placeholder="Enter semester" name="semester" required>
          </div>
          <div class="b2x">
            <span class="details">Shift (M or E)</span>
            <input type="text" placeholder="Enter Shift" name="shift" required>
          </div>
        </div>
        <div class="button">
          <input type="submit" value="Register">
        </div>
      </form>
    </div>
  </div>

</body>
</html>

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.

db/db.php

<?php
   // define database related variables
	$servername = "localhost";
	$username = "your username";
	$password = "your password";
	$dbname = "database name";
   // try to conncet to database
   try 
	{
		$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.

registered.php

<?php
    session_start();
    date_default_timezone_set('Asia/Kolkata');
    require_once('db/db.php');
?>

<?php
$fname=$_POST['fname'];
$email=$_POST['email'];
$contact=$_POST['contact'];
$enroll=$_POST['enroll'];
$course=$_POST['course'];
$semester=$_POST['semester'];
$shift=$_POST['shift'];

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

$_SESSION['fname']=$fname;
$_SESSION['email']=$email;
$_SESSION['contact']=$contact;
$_SESSION['enroll']=$enroll;
$_SESSION['course']=$course;
$_SESSION['semester']=$semester;
$_SESSION['shift']=$shift;
$_SESSION['dt']=$dt;

if($shift=='M')
  $sh=1;
else
  $sh=2;
?>
<?php
$sql="insert into dea(enrol, pname, email, contactno, course, sem, shift, regdate) values(:ct, :sn, :em, :cn, :pg, :cr, :qr, :qd)";
$stmt=$con1->prepare($sql);
$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);
$ex=$stmt->execute();

if($ex)
{
    $res= 'Thank You!';
}
else
{
     $res= 'Temporary Error! Please try after some time.';
}

?>

Output

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

programmingempire

You may also like...