PHP - Simple Upload XML File to MySQLi

Submitted by: 
Language: 
Visitors have accessed this post 233 times.

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.