Insert Form Data Using PDO In PHP | CampCodes

Insert Form Data Using PDO in PHP

June 9, 2020
insert form data using pdo in php

In this tutorial, we will create a Simple Insert Form Data using PDO. This code can insert a form of data to the database server with a PDO query. The system uses a PDO insert query to store the data inputs to a database server with a safety feature for avoiding injection tools.

We will be using PDO as a query scripting it an acronym for PHP Data Objects. It is a lean, clean, and consistent way to access databases. This means developers can write portable code much more accessible.

Table of Contents

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server

And, this is the link for the bootstrap that I used for the layout design

Creating Database

Open your database web server then create a database name in it db_insert_pdo; after that, click Import, then locate the database file inside the folder of the application then click ok.

insert form data using pdo in php

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

    $conn = new PDO( 'mysql:host=localhost;dbname=db_insert_pdo', 'root', '' );
        die("Error: Failed to connect to database!");

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

<!DOCTYPE html>
<html lang="en">
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a href="" class="navbar-brand">Sourcecodester</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Simple Insert Form Data Using PDO</h3>
        <hr style="border-top:1px dotted #ccc;" />
        <div class="col-md-1"></div>
        <div class="col-md-10">
            <button class="btn btn-success" type="button" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add user</button>
            <br /><br />
            <table class="table table-bordered">
                <thead class="alert-info">
                        require 'conn.php';
                        $query = $conn->prepare("SELECT * FROM `user` ORDER BY `user_id` DESC");
                        while($fetch = $query->fetch()){
                            <td><?php echo $fetch['firstname']?></td>
                            <td><?php echo $fetch['lastname']?></td>
                            <td><?php echo $fetch['username']?></td>
<div class="modal fade" id="form_modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <form method="POST" action="insert.php">
                <div class="modal-header">
                    <h3 class="modal-title">Add User</h3>
                <div class="modal-body">
                    <div class="col-md-2"></div>
                    <div class="col-md-8">
                        <div class="form-group">
                            <input class="form-control" type="text" name="firstname"/>
                        <div class="form-group">
                            <input class="form-control" type="text" name="lastname"/>
                        <div class="form-group">
                            <input class="form-control" type="text" name="username"/>
                        <div class="form-group">
                            <input class="form-control" type="password" name="password"/>
                <br style="clear:both;"/>
                <div class="modal-footer">
                    <button class="btn btn-danger" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
                    <button class="btn btn-primary" name="insert"><span class="glyphicon glyphicon-save"></span> Save</button>





Creating the Main Function

This code contains the main function of the application. This code will store the data inputs to the database server when the button is clicked. To do this just kindly write these block of codes inside the text editor, then save it as insert.php.

    require_once 'conn.php';
            $firstname = $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $username = $_POST['username'];
            $password = $_POST['password'];
            $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "INSERT INTO `user` (`firstname`, `lastname`, `username`, `password`) VALUES ('$firstname', '$lastname', '$username', '$password')";
        }catch(PDOException $e){
            echo $e->getMessage();
        $conn = null;
        echo "<script>alert('Successfully inserted data!')</script>";
        echo "<script>window.location='index.php'</script>";
There you have it we successfully created a Simple Insert Form Data using PDO. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!


Leave a Reply

Your email address will not be published. Required fields are marked *