Table of Contents
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 https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.
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.
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.
<?php $conn = new PDO( 'mysql:host=localhost;dbname=db_pdo_array', 'root', ''); if(!$conn){ 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"> <head> <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/> </head> <body> <nav class="navbar navbar-default"> <div class="container-fluid"> <a href="https://sourcecodester.com" class="navbar-brand">CampCodes</a> </div> </nav> <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"> <tr> <th>Firstname</th> <th>Lastname</th> <th>Address</th> </tr> </thead> <tbody> <?php require'conn.php'; $query = $conn->prepare("SELECT * FROM `member`"); $query->execute(); while($row = $query->fetch()){ echo "<tr> <td>".$row['firstname']."</td> <td>".$row['lastname']."</td> <td>".$row['address']."</td> </tr>"; } ?> </tbody> </table> </div> <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> </form> <br /><br /> <?php $array=array( array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"), array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"), ); echo"<pre>"; print_r($array); echo"</pre>" ?> </div> </body> </html>
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.
<?php require_once 'conn.php'; if(ISSET($_POST['import'])){ $array=array( array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"), array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"), array("firstname"=>"Tony", "lastname"=>"Stark", "address"=>"Socovia"), ); $count=count($array); $query=$conn->prepare("SELECT COUNT(*) as total FROM `member`"); $query->execute(); $row=$query->fetch(); if($row['total']==0){ for($i=0; $i<$count-1; $i++){ $firstname=$array[$i]["firstname"]; $lastname=$array[$i]["lastname"]; $address=$array[$i]["address"]; try{ $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname', '$lastname', '$address')"; $conn->exec($sql); }catch(PDOException $e){ echo $e->getMessage(); } } echo"<script>alert('Successfully imported!')</script>"; echo"<script>window.location='index.php'</script>"; }else{ echo"<script>alert('An array is already imported!')</script>"; echo"<script>window.location='index.php'</script>"; } } ?>