PHP - Store JSON Data To MySQLi

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

In this tutorial we will create a Store JSON Data To MySQLi using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Before we get 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.

Lastly, 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_json, 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 = new mysqli('localhost', 'root', '', 'db_json');
  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 - Store JSON Data To MySQLi</h3>
  17. <hr style="border-top:1px dotted #ccc;"/>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <form method="POST" action="upload.php" enctype="multipart/form-data">
  21. <div class="form-group">
  22. <input type="file" name="filename" class="form-control"/>
  23. </div>
  24. <div class="form-group">
  25. <button name="upload" class="btn btn-primary form-control"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  26. </div>
  27. </form>
  28. </div>
  29. <table class="table table-bordered">
  30. <thead class="alert-success">
  31. <tr>
  32. <th>Name</th>
  33. <th>Gender</th>
  34. <th>Address</th>
  35. </tr>
  36. </thead>
  37. <tbody style="background-color:#fff;">
  38. <?php
  39. require 'conn.php';
  40.  
  41. $query = $conn->query("SELECT * FROM `employee` ORDER BY `lastname` ASC");
  42. while($fetch = $query->fetch_array()){
  43.  
  44. ?>
  45. <tr>
  46. <td><?php echo $fetch['firstname']." ".$fetch['lastname']?></td>
  47. <td><?php echo $fetch['gender']?></td>
  48. <td><?php echo $fetch['address']?></td>
  49. </tr>
  50. <?php
  51. }
  52. ?>
  53. </tbody>
  54. </table>
  55. </div>
  56. </body>
  57. </html>

Creating Main Function

This is where the main function of the application. This code will store the JSON data to MySQLi database when the file is uploaded successfully. To do this just copy and write these block of codes inside the text editor, then save it as upload.php.

  1. <?php
  2. require 'conn.php';
  3.  
  4. if(ISSET($_POST['upload'])){
  5. $file = explode(".", $_FILES['filename']['name']);
  6. $ext = end($file);
  7.  
  8. if($ext == "json"){
  9. $data = file_get_contents($_FILES['filename']['tmp_name']);
  10. $array = json_decode($data, true);
  11. foreach($array as $row){
  12. $conn->query("INSERT INTO `employee` VALUES('', '$row[firstname]', '$row[lastname]', '$row[gender]', '$row[address]')");
  13. }
  14. header('location: index.php');
  15. }else{
  16. echo "<script>alert('Only JSON file is available')</script>";
  17. echo "<script>window.location = 'index.php'</script>";
  18. }
  19. }
  20. ?>

There you have it we successfully created Store JSON Data 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.