Upload File .csv ke mysql
1. Index .php
.
# form harus di beri kode enctype="multipart/form-data" jika ingin membuat form upload file.
2. koneksi.php
3. pr_upload.php
# tiap kolom di .csv / excel di masukkan di variabel $a1,$a2 melalui array $filesop[n]
$filesop[0] = kolom 1 baris 1 di file .csv
$filesop[1] = kolom 2 baris 1 di file .csv
4. buat database
.
Referensi .
<form action="pr_upload.php" method="post" enctype="multipart/form-data">
<h2>Input Data</h2>
<input type="file" name="diupload" id="fileupload">
<input type="submit" name="klikupload" value="proses upload">
</form>
Input Data
.
# form harus di beri kode enctype="multipart/form-data" jika ingin membuat form upload file.
2. koneksi.php
$uname='root';.
$pwd='';
$host='localhost';
$db='tryupload';
$con=mysql_connect($host,$uname,$pwd);
$con_2=mysql_select_db($db);
if(!$con || !$con_2){
echo "Gagal konek ke database";
}
3. pr_upload.php
include "koneksi.php";.
echo "a";
if(isset($_POST['klikupload'])){
$file=$_FILES["diupload"]["tmp_name"];
echo "b";
echo "$file";
$c=0;
$handle=fopen($file,"r");
echo "c";
while(($filesop= fgetcsv($handle,1000,",")) !== false){
$a1=$filesop[0];
$a2=$filesop[1];
echo "d";
$qer=mysql_query("insert into user values('".$a1."','".$a2."')");
echo "f";
}
echo "g";
if($qer){
echo "data sukses di input";
}else{
echo "data gagal di input";
}
}
# tiap kolom di .csv / excel di masukkan di variabel $a1,$a2 melalui array $filesop[n]
$filesop[0] = kolom 1 baris 1 di file .csv
$filesop[1] = kolom 2 baris 1 di file .csv
4. buat database
.
Referensi .
makasi bro (o)
ReplyDelete