How to Create a Personal details information program using PHP PDO OOP

 

Create a Personal details information program using PHP PDO OOP

In this tutorial, we are going to learn about creating a personal information program using PHP, PDO, and OOP.

PHP PDO OOP can be a great way to store and retrieve personal information in a secure. The first step is going to create an HTML form and simple program that allows users to input their personal information, and then display the stored data in a table.

And then create a new PHP file, and define a class called 'personalInfo'. The class will use the PDO object passed to its constructor and the constructor is interact with the database.

HTML Form allows user to input their personal information and submit the form data and the PHP file class function saved the data in the database.

Create a Database Table:



create a table personal_info
(
id int primary key auto_increment, 
first_name varchar(50) not null,
last_name varchar(50) not null,
email varchar(100) not null,
address varchar(255) not null
);


HTML FORM:



<div class="container py-5">
    <h1 class="text-center py-3">Person Details Data in PHP PDO OOP</h1>
    <form method="post" class="form-control p-5" style="width:50%;margin:0 auto;">
	    <div class="mb-3">
        <label for="first_name">First Name:</label>
        <input type="text" class="form-control" id="first_name" name="first_name" required>
        </div>
		<br>
		<div class="mb-3">
        <label for="last_name">Last Name:</label>
        <input type="text" id="last_name" class="form-control" name="last_name" required>
        </div>
		<br>
		<div class="mb-3">
        <label for="email">Email:</label>
        <input type="email" id="email" class="form-control" name="email" required>
        </div>
		<br>
		<div class="mb-3">
        <label for="address">Address:</label>
        <input type="text" id="address" class="form-control" name="address" required>
        </div>
		<br>
        <input class="btn btn-lg btn-success" type="submit" value="Submit" name="submit">
		<input class="btn btn-lg btn-danger" type="reset" value="Reset" name="reset">
    </form>
    <div class="py-5"></div>


Create a Personal details information program using PHP PDO OOP


Retrieve Personal Details Information:



<table class="table table-bordered">
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Address</th>
        </tr>
        <?php foreach ($data as $row) { ?>
            <tr>
                <td><?php echo $row['first_name'] ?></td>
                <td><?php echo $row['last_name'] ?></td>
                <td><?php echo $row['email'] ?></td>
                <td><?php echo $row['address'] ?></td>
            </tr>
        <?php } ?>
    </table>
</div>

Create a Personal details information program using PHP PDO OOP


PHP PDO OOP Class:



class PersonalInfo {
    private $pdo;

    public function __construct($pdo) {
        $this->pdo = $pdo;
    }

    public function create($first_name, $last_name, $email, $address) {
        $stmt = $this->pdo->prepare("INSERT INTO personal_info (first_name, last_name, email, address) VALUES (:fname, :lname ,:email ,:address )");
        $stmt->bindParam(':fname', $first_name);
        $stmt->bindParam(':lname', $last_name);
        $stmt->bindParam(':email', $email);
		$stmt->bindParam(':address', $address);
		
        $stmt->execute();
    }

    public function read() {
        $stmt = $this->pdo->prepare("SELECT * FROM personal_info");
        $stmt->execute();
        return $stmt->fetchAll();
    }
}

        
try {		
    $pdo = new PDO('mysql:host=localhost;dbname=webontools', 'root', '');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} 
  catch(PDOException $e) {
           echo $e->getMessage();
    }

$info = new PersonalInfo($pdo);

if (isset($_POST['submit'])) {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $address = $_POST['address'];
    $info->create($first_name, $last_name, $email, $address);
}

$data = $info->read();

Finally, we will use the 'PersonalInfo' class's 'create' method to insert the personal information into the MySQL.I hope you will learn and understanding the PHP OOP in this example.




Previous Post Next Post