PHP - Simple Import Array To MySQLi

In this tutorial we will create a Simple Import Array To MySQLi using PHP. This code can import an array of data to the database server when user click the import button. The code use MySQLi INSERT query to store an array object to the database server by using for loop to break down the array as a readable string format. This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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_array, after that click Import then locate the database file inside the folder of the application then click ok. tut1

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.
  1. <?php
  2.         $conn=mysqli_connect("localhost", "root", "", "db_array");
  3.        
  4.         if(!$conn){
  5.                 die("Error: Failed to connect to database");
  6.         }
  7. ?>

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.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a href="https://sourcecodester.com" class="navbar-brand">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Simple Import Array To MySQLi</h3>
  16.                 <hr style="border-top:1px dotted #ccc;" />
  17.                 <a class="btn btn-primary" href="import.php"><span class="glyphicon glyphicon-import"></span> Import</a>
  18.                 <br /><br />
  19.                 <div class="col-md-5">
  20.                         <?php
  21.                                 $array=array(
  22.                                         array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"),
  23.                                         array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"),
  24.                                 );
  25.                                
  26.                                 echo"<pre>";
  27.                                 print_r($array);
  28.                                 echo"</pre>"
  29.                         ?>
  30.                 </div>
  31.                 <div class="col-md-1"></div>
  32.                 <div class="col-md-6">
  33.                         <table class="table table-bordered">
  34.                                 <thead class="alert-info">
  35.                                         <tr>
  36.                                                 <th>Firstname</th>
  37.                                                 <th>Lastname</th>
  38.                                                 <th>Address</th>
  39.                                         </tr>
  40.                                 </thead>
  41.                                 <tbody>
  42.                                         <?php
  43.                                                 require'conn.php';
  44.                                                 $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  45.                                                 while($fetch=mysqli_fetch_array($query)){
  46.                                         ?>
  47.                                         <tr>
  48.                                                 <td><?php echo$fetch['firstname']?></td>
  49.                                                 <td><?php echo$fetch['lastname']?></td>
  50.                                                 <td><?php echo$fetch['address']?></td>
  51.                                         </tr>
  52.                                         <?php
  53.                                                 }
  54.                                         ?>
  55.                                 </tbody>
  56.                         </table>
  57.                 </div>
  58.                
  59. </body>
  60. </html>

Creating the Main Function

This code contains the main function of the application. This code will import an array when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as import.php
  1. <?php
  2.         require_once'conn.php';
  3.        
  4.         $array=array(
  5.                 array("firstname"=>"John", "lastname"=>"Smith", "address"=>"New York"),
  6.                 array("firstname"=>"Claire", "lastname"=>"Temple", "address"=>"Racoon City"),
  7.                 array("firstname"=>"Tony", "lastname"=>"Stark", "address"=>"Socovia"),
  8.         );
  9.        
  10.         $count=count($array);
  11.         $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  12.         $row=mysqli_num_rows($query);
  13.        
  14.         if($row==0){
  15.                 for($i=0; $i<$count-1; $i++){
  16.                         $firstname=$array[$i]["firstname"];
  17.                         $lastname=$array[$i]["lastname"];
  18.                         $address=$array[$i]["address"];
  19.                         mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  20.                 }
  21.                
  22.                 echo"<script>alert('Successfully imported!')</script>";
  23.                 echo"<script>window.location='index.php'</script>";
  24.                
  25.         }else{
  26.                 echo"<script>alert('An array is already imported!')</script>";
  27.                 echo"<script>window.location='index.php'</script>";
  28.         }
  29.        
  30. ?>
There you have it we successfully created Simple Import Array To MySQLi using PHP. 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!

Add new comment