Insert Array Data Into Database Using PDO In PHP | CampCodes

Insert Array Data Into Database Using PDO in PHP

December 30, 2020
insert array data into database using pdo in php

Tutorial: Insert Array Data Into Database Using PDO in PHP with Free Source Code

In this tutorial, we will create an Insert Array Data Into Database using PDO. This code will insert an array of data to the database server when the user clicks the import button. The system uses the PHP POST method to call a species that will add the array data by break it down one by one with the use of for() loop before inserting it through the PDO query. This a user-friendly program. Feel free to modify and use it for your system.

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 comfortable.

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_pdo_array; after that click Import, then locate the database file inside the folder of the application then click ok.

insert array data into database 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_pdo_array', 'root', '');
        die("Error: Failed to coonect 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>
<!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">CampCodes</a>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Insert Array Data Into Database Using PDO</h3>
        <hr style="border-top:1px dotted #ccc;" />
        <div class="col-md-6">
            <table class="table table-bordered">
                <thead class="alert-info">
                        $query = $conn->prepare("SELECT * FROM `member`");
                        while($row = $query->fetch()){
                            echo "<tr>
        <div class="col-md-1"></div>
        <div class="col-md-5">
            <form method="POST" action="import.php">
                <button class="btn btn-primary" name="import"><span class="glyphicon glyphicon-import"></span> Import</button>
            <br /><br />
                    array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"),
                    array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"),

Creating the Main Function

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

    require_once 'conn.php';
            array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"),
            array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"),
            array("firstname"=>"Tony", "lastname"=>"Stark", "address"=>"Socovia"),
        $query=$conn->prepare("SELECT COUNT(*) as total FROM `member`");
            for($i=0; $i<$count-1; $i++){
                    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                    $sql = "INSERT INTO `member`(firstname, lastname, address)  VALUES ('$firstname', '$lastname', '$address')";
                }catch(PDOException $e){
                    echo $e->getMessage();
            echo"<script>alert('Successfully imported!')</script>";
            echo"<script>alert('An array is already imported!')</script>";
There you have it we successfully created a Insert Array Data Into Database 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 *