• We just launched and are currently in beta. Join us as we build and grow the community.

PHP - Store JSON Data To MySQLi

RalphLauren

Sales Funnel Architect
R Rep
0
0
0
Rep
0
R Vouches
0
0
0
Vouches
0
Posts
181
Likes
88
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.
2018-07-19_13_06_53-localhost_127.0.0.1_db_json_phpmyadmin_4.7.0.png

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. if

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  6. ?>

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 - Store JSON Data To MySQLi</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-3"></div>
  18. <div class="col-md-6">
  19. <form method="POST" action="upload.php" enctype="multipart/form-data">
  20. <div class="form-group">
  21. <input type="file" name="filename" class="form-control"/>
  22. </div>
  23. <div class="form-group">
  24. <button name="upload" class="btn btn-primary form-control"><span class="glyphicon glyphicon-upload"></span> Upload</button>
  25. </div>
  26. </form>
  27. </div>
  28. <table class="table table-bordered">
  29. <thead class="alert-success">
  30. <tr>
  31. <th>Name</th>
  32. <th>Gender</th>
  33. <th>Address</th>
  34. </tr>
  35. </thead>
  36. <tbody style="background-color:#fff;">
  37. <?php
  38. require

    'conn.php'

    ;

  39. $query

    =

    $conn

    ->

    query

    (

    "SELECT * FROM `employee` ORDER BY `lastname` ASC"

    )

    ;
  40. while

    (

    $fetch

    =

    $query

    ->

    fetch_array

    (

    )

    )

    {

  41. ?>
  42. <tr>
  43. <td><?php

    echo

    $fetch

    [

    'firstname'

    ]

    .

    " "

    .

    $fetch

    [

    'lastname'

    ]

    ?>

    </td>
  44. <td><?php

    echo

    $fetch

    [

    'gender'

    ]

    ?>

    </td>
  45. <td><?php

    echo

    $fetch

    [

    'address'

    ]

    ?>

    </td>
  46. </tr>
  47. <?php
  48. }
  49. ?>
  50. </tbody>
  51. </table>
  52. </div>
  53. </body>
  54. </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. if

    (

    ISSET

    (

    $_POST

    [

    'upload'

    ]

    )

    )

    {
  4. $file

    =

    explode

    (

    "."

    ,

    $_FILES

    [

    'filename'

    ]

    [

    'name'

    ]

    )

    ;
  5. $ext

    =

    end

    (

    $file

    )

    ;

  6. if

    (

    $ext

    ==

    "json"

    )

    {
  7. $data

    =

    file_get_contents

    (

    $_FILES

    [

    'filename'

    ]

    [

    'tmp_name'

    ]

    )

    ;
  8. $array

    =

    json_decode

    (

    $data

    ,

    true

    )

    ;
  9. foreach

    (

    $array

    as

    $row

    )

    {
  10. $conn

    ->

    query

    (

    "INSERT INTO `employee` VALUES('', '$row[firstname]

    ', '$row[lastname]

    ', '$row[gender]

    ', '$row[address]

    ')"

    )

    ;
  11. }
  12. header

    (

    'location: index.php'

    )

    ;
  13. }

    else

    {
  14. echo

    "<script>alert('Only JSON file is available')</script>"

    ;
  15. echo

    "<script>window.location = 'index.php'</script>"

    ;
  16. }
  17. }
  18. ?>

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!!!


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,292

324,186

324,194

Top