Import Data from XML file to PHP and MySQL


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 */;




Create a xml file containing the product information you need to import

<?xml version="1.0" encoding="utf-8" ?>
<products>
  <product>
    <id>p01</id>
    <name>Name 1</name>
    <price>100</price>
    <quantity>2</quantity>
  </product>
  <product>
    <id>p02</id>
    <name>Name 2</name>
    <price>200</price>
    <quantity>3</quantity>
  </product>
  <product>
    <id>p03</id>
    <name>Name 3</name>
    <price>300</price>
    <quantity>5</quantity>
  </product>
  <product>
    <id>p04</id>
    <name>Name 4</name>
    <price>110</price>
    <quantity>7</quantity>
  </product>
</products>

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', '');
?>

Create PHP file named index.php. This file will allow the user to select the xml file and import the data into the MySQL database

<?php
require 'database.php';
if(isset($_POST['buttonImport'])) {
	copy($_FILES['xmlFile']['tmp_name'],
		'data/'.$_FILES['xmlFile']['name']);
	$products = simplexml_load_file('data/'.$_FILES['xmlFile']['name']);
	foreach($products as $product){
		$stmt = $conn->prepare('insert into
			product(id, name, price, quantity)
			values(:id, :name, :price, :quantity)');
		$stmt->bindValue('id', $product->id);
		$stmt->bindValue('name', $product->name);
		$stmt->bindValue('price', $product->price);
		$stmt->bindValue('quantity', $product->quantity);
		$stmt->execute();
	}
}
$stmt = $conn->prepare('select * from product');
$stmt->execute();
?>

<form method="post" enctype="multipart/form-data">
	XML File <input type="file" name="xmlFile">
	<br>
	<input type="submit" value="Import" name="buttonImport">
</form>
<br>
<h3>Product List</h3>
<table cellpadding="2" cellspacing="2" border="1">
	<tr>
		<th>Id</th>
		<th>Name</th>
		<th>Price</th>
		<th>Quantity</th>
	</tr>
	<?php while($product = $stmt->fetch(PDO::FETCH_OBJ)) { ?>
	<tr>
		<td><?php echo $product->id; ?></td>
		<td><?php echo $product->name; ?></td>
		<td><?php echo $product->price; ?></td>
		<td><?php echo $product->quantity; ?></td>
	</tr>
	<?php } ?>
</table>




Import Page

Product Table After Import

I recommend you refer to the books below to learn more about the knowledge in this article: