Import Data Dari CSV ke MySQL dengan PHP Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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