Database
Create a new MySQL database named demo and execute the SQL code below:
-- phpMyAdmin SQL Dump
-- version 4.6.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Sep 30, 2017 at 05:31 AM
-- Server version: 5.7.14
-- PHP Version: 7.0.10
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `demo`
--
-- --------------------------------------------------------
--
-- Table structure for table `product`
--
CREATE TABLE `product` (
`id` int(11) NOT NULL,
`name` varchar(250) NOT NULL,
`price` decimal(10,2) NOT NULL,
`quantity` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `product`
--
INSERT INTO `product` (`id`, `name`, `price`, `quantity`) VALUES
(1, 'mobile 1', '20.00', 7),
(2, 'computer 1', '1.00', 3),
(3, 'computer 2', '1.00', 3),
(4, 'computer 3', '1.00', 3),
(5, 'fashion 1', '1.00', 3),
(6, 'fashion 2', '1.00', 3),
(7, 'fashion 3', '1.00', 3),
(8, 'fashion 4', '1.00', 3);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `product`
--
ALTER TABLE `product`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `product`
--
ALTER TABLE `product`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
JSON File
Create a json file containing the product information you need to import
<?php
[
{"id":1, "name":"mobile 1", "price":20.00, "quantity":7},
{"id":3, "name":"computer 1", "price":1.00, "quantity":3},
{"id":8, "name":"computer 2", "price":1.00, "quantity":3},
{"id":9, "name":"computer 3", "price":1.00, "quantity":3},
{"id":10, "name":"fashion 1", "price":1.00, "quantity":3},
{"id":11, "name":"fashion 2", "price":1.00, "quantity":3},
{"id":12, "name":"fashion 3", "price":1.00, "quantity":3},
{"id":13, "name":"fashion 4", "price":1.00, "quantity":3}
]
?>
Database Connection
Create PHP file named connect.php. Use PDO connect to demo database with default account:
Username: root
Password:
<?php
$conn = new PDO("mysql:host=localhost;dbname=demo", 'root', '');
?>
Import Page
Create PHP file named index.php. This page allows the user to select the json file and import the data into the database
<?php
require 'database.php';
if(isset($_POST['buttomImport'])) {
copy($_FILES['jsonFile']['tmp_name'], 'jsonFiles/'.$_FILES['jsonFile']['name']);
$data = file_get_contents('jsonFiles/'.$_FILES['jsonFile']['name']);
$products = json_decode($data);
foreach ($products as $product) {
$stmt = $conn->prepare('insert into product(name, price, quantity) values(:name, :price, :quantity)');
$stmt->bindValue('name', $product->name);
$stmt->bindValue('price', $product->price);
$stmt->bindValue('quantity', $product->quantity);
$stmt->execute();
}
}
?>
<html>
<head>
<title>Import JSON File</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
JSON File <input type="file" name="jsonFile">
<br>
<input type="submit" value="Import" name="buttomImport">
</form>
</body>
</html>
Demo
Import Page
Product Table Before Import
Product Table After Import
References
I recommend you refer to the books below to learn more about the knowledge in this article:
- Murach’s PHP and MySQL (3rd Edition)
- Learning PHP, MySQL & JavaScript: With jQuery, CSS & HTML5 (Learning Php, Mysql, Javascript, Css & Html5)
- PHP and MySQL Web Development (5th Edition) (Developer’s Library)
- Murach’s MySQL, 2nd Edition
- MySQL (5th Edition) (Developer’s Library)
- PHP Ajax Cookbook