PHP - Simple Upload XML File to MySQLi

In this tutorial we will create a Simple Upload XML File to MySQLi using PHP. This code can upload XML file to a data to MySQLi when user click the upload button. The code use simplexml_load_file to read a block of data from the uploaded xml file in order to break down in a loop and send to MySQLi server. This is a user-friendly kind of program feel free to modify it. We will be using XML as a markup language that utilize in php as a HTML data. It is designed to store and transport data that can be manipulated within the local server.

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_xml, 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_xml");
  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.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href = "https://sourcecodester.com">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 Upload XML File to MySQLi</h3>
  16.                 <hr style="border-top:1px dotted #000;" />
  17.                 <div class="col-md-4">
  18.                         <form method="POST" action="upload.php" enctype="multipart/form-data">
  19.                                 <h5>Upload XML here</h5>
  20.                                 <input type="file" name="file"/>
  21.                                 <br />
  22.                                 <center><button name="upload" class="btn btn-primary">Upload</button></center>
  23.                         </form>
  24.                 </div>
  25.                 <div class="col-md-8">
  26.                         <table class="table table-bordered">
  27.                                 <thead class="alert-info">
  28.                                         <tr>
  29.                                                 <th>Firstname</th>
  30.                                                 <th>Lastname</th>
  31.                                                 <th>Address</th>
  32.                                         </tr>
  33.                                 </thead>
  34.                                 <tbody>
  35.                                         <?php
  36.                                                 require'conn.php';
  37.                                                
  38.                                                 $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  39.                                                 while($fetch=mysqli_fetch_array($query)){
  40.                                         ?>
  41.                                                 <tr>
  42.                                                         <td><?php echo $fetch['firstname']?></td>
  43.                                                         <td><?php echo $fetch['lastname']?></td>
  44.                                                         <td><?php echo $fetch['address']?></td>
  45.                                                 </tr>
  46.                                         <?php
  47.                                                 }
  48.                                         ?>
  49.                                 </tbody>
  50.                         </table>
  51.                 </div>
  52.         </div>
  53. </body>
  54. </html>

Creating the Main Function

This code contains the main function of the application. This code will upload a xml file to database server 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 upload.php.
  1. <?php
  2.         require_once'conn.php';
  3.         if(ISSET($_POST['upload'])){
  4.                 $file_name=$_FILES['file']['name'];
  5.                 $exp=explode('.', $file_name);
  6.                 $name=end($exp);
  7.                
  8.                 if($name=="xml"){
  9.                         $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  10.                         $count=mysqli_num_rows($query);
  11.                        
  12.                         if($count >0){
  13.                                 echo"<script>alert('XML file is already uploaded!')</script>";
  14.                                 echo"<script>window.location='index.php'</script>";
  15.                         }else{
  16.                                 $xml = simplexml_load_file(''.$file_name);
  17.                                 foreach($xml->member as $member){
  18.                                         $firstname=$member->firstname;
  19.                                         $lastname=$member->lastname;
  20.                                         $address=$member->address;
  21.                                        
  22.                                         mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  23.                                 }
  24.                                
  25.                                 header('location:index.php');
  26.                         }
  27.  
  28.                 }else{
  29.                         echo"<script>alert('This is not a XML file')</script>";
  30.                                 echo"<script>window.location='index.php'</script>";
  31.                 }
  32.         }
  33. ?>
There you have it we successfully created Simple Upload XML File 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