001.
<?php
002.
$conn
= mysqli_connect(
"localhost"
,
"root"
,
"12345678"
,
"DB001"
);
003.
004.
005.
date_default_timezone_set(
"Asia/Bangkok"
);
006.
007.
$trn_date
=
date
(
"Y-m-d H:i:s"
);
008.
009.
010.
if
(isset(
$_POST
[
"import"
])) {
011.
012.
$fileName
=
$_FILES
[
"file"
][
"tmp_name"
];
013.
014.
if
(
$_FILES
[
"file"
][
"size"
] > 0) {
015.
016.
$file
=
fopen
(
$fileName
,
"r"
);
017.
018.
while
((
$column
=
fgetcsv
(
$file
, 1000,
","
)) !== FALSE) {
019.
$sqlInsert
= "INSERT INTO users (id, Name)
020.
values (
'" . $column[0] . "'
,
'" . $column[1] . "'
)";
021.
$result
= mysqli_query(
$conn
,
$sqlInsert
);
022.
023.
if
(!
empty
(
$result
)) {
024.
$type
=
"success"
;
025.
$message
=
"CSV Data Imported into the Database"
;
026.
}
else
{
027.
$type
=
"error"
;
028.
$message
=
"Problem in Importing CSV Data"
;
029.
}
030.
}
031.
}
032.
}
033.
?>
034.
<!DOCTYPE html>
035.
<html>
036.
037.
<head>
038.
<script src=
"jquery-3.2.1.min.js"
></script>
039.
040.
<style>
041.
body {
042.
font-family: Arial;
043.
width: 1000;
044.
}
045.
046.
.outer-scontainer {
047.
background: #F0F0F0;
048.
border: #e0dfdf 1px solid;
049.
padding: 20px;
050.
border-radius: 2px;
051.
}
052.
053.
.input-row {
054.
margin-top: 0px;
055.
margin-bottom: 20px;
056.
}
057.
058.
.btn-submit {
059.
background: #333;
060.
border: #1d1d1d 1px solid;
061.
color: #f0f0f0;
062.
font-size: 0.9em;
063.
width: 100px;
064.
border-radius: 2px;
065.
cursor: pointer;
066.
}
067.
068.
.outer-scontainer table {
069.
border-collapse: collapse;
070.
width: 100%;
071.
}
072.
073.
.outer-scontainer th {
074.
border: 1px solid #dddddd;
075.
padding: 8px;
076.
text-align: left;
077.
}
078.
079.
.outer-scontainer td {
080.
border: 1px solid #dddddd;
081.
padding: 8px;
082.
text-align: left;
083.
}
084.
085.
#response {
086.
padding: 10px;
087.
margin-bottom: 10px;
088.
border-radius: 2px;
089.
display:none;
090.
}
091.
092.
.success {
093.
background: #c7efd9;
094.
border: #bbe2cd 1px solid;
095.
}
096.
097.
.error {
098.
background: #fbcfcf;
099.
border: #f3c6c7 1px solid;
100.
}
101.
102.
div#response.display-block {
103.
display: block;
104.
}
105.
</style>
106.
<script type=
"text/javascript"
>
107.
$(document).ready(
function
() {
108.
$(
"#frmCSVImport"
).on(
"submit"
,
function
() {
109.
110.
$(
"#response"
).attr(
"class"
,
""
);
111.
$(
"#response"
).html(
""
);
112.
var
fileType
=
".csv"
;
113.
var
regex =
new
RegExp(
"([a-zA-Z0-9\s_\\.\-:])+("
+
fileType
+
")$"
);
114.
if
(!regex.test($(
"#file"
).val().toLowerCase())) {
115.
$(
"#response"
).addClass(
"error"
);
116.
$(
"#response"
).addClass(
"display-block"
);
117.
$(
"#response"
).html(
"Invalid File. Upload : <b>"
+
fileType
+
"</b> Files."
);
118.
return
false;
119.
}
120.
return
true;
121.
});
122.
});
123.
</script>
124.
</head>
125.
126.
<body>
127.
<h2>Import CSV file.</h2>
128.
129.
<div id=
"response"
class
=
"<?php if(!empty($type)) { echo $type . "
display-block
"; } ?>"
><?php
if
(!
empty
(
$message
)) {
echo
$message
; } ?></div>
130.
<div
class
=
"outer-scontainer"
>
131.
<div
class
=
"row"
>
132.
133.
<form
class
=
"form-horizontal"
action=
""
method=
"post"
134.
name=
"frmCSVImport"
id=
"frmCSVImport"
enctype=
"multipart/form-data"
>
135.
<div
class
=
"input-row"
>
136.
<label
class
=
"col-md-4 control-label"
>Choose CSV
137.
File</label> <input type=
"file"
name=
"file"
138.
id=
"file"
accept=
".csv"
>
139.
<button type=
"submit"
id=
"submit"
name=
"import"
140.
class
=
"btn-submit"
>Import</button>
141.
<br />
142.
143.
</div>
144.
145.
</form>
146.
147.
</div>
148.
<?php
149.
$sqlSelect
=
"SELECT * FROM users"
;
150.
$result
= mysqli_query(
$conn
,
$sqlSelect
);
151.
152.
if
(mysqli_num_rows(
$result
) > 0) {
153.
?>
154.
<table id=
'userTable'
>
155.
<thead>
156.
<tr>
157.
<th>id</th>
158.
<th>Name</th>
159.
160.
161.
162.
</tr>
163.
</thead>
164.
<?php
165.
166.
while
(
$row
= mysqli_fetch_array(
$result
)) {
167.
?>
168.
169.
<tbody>
170.
<tr>
171.
172.
<td><?php
echo
$row
[
'id'
]; ?></td>
173.
<td><?php
echo
$row
[
'Name'
]; ?></td>
174.
175.
176.
177.
</tr>
178.
<?php
179.
}
180.
?>
181.
</tbody>
182.
</table>
183.
<?php } ?>
184.
</div>
185.
186.
</body>
187.
188.
</html>