Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@333crist
Last active April 21, 2018 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 333crist/9163e9b545adadf738159021393a6953 to your computer and use it in GitHub Desktop.
Save 333crist/9163e9b545adadf738159021393a6953 to your computer and use it in GitHub Desktop.
Import Data Dari CSV ke MySQL dengan PHP Native
<h2>Upload Biodata</h2>
<?php
ini_set('max_execution_time', 3000);
if (!$_POST) { ?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Choose your file:
<input name="csv" type="file" id="csv" />
<br>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
} else {
$connect = new mysqli("localhost", "root", "", "tb_biodata");
if ($_FILES['csv']['size'] > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file, "r");
$i = 0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
if ($i > 0) {
$import = "INSERT into tb_biodata(
id,nama,usia,domisili,create_update)
values(
'',
'$data[0]',
'$data[1]',
'$data[2]',
now()
)";
$connect->query($import);
}
$i++;
}
fclose($handle);
print "Import done";
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Choose your file:
<input name="csv" type="file" id="csv" />
<br>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
}
else
{
?>
<html>
<body>
<form action="" method="post" enctype="multipart/form-data">
Choose your file:
<input name="csv" type="file" id="csv" />
<br>
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>
<?php
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment