Upload, analyze dan import CSV ke MySQL dengan PHP

Rintaka Jayadi Brata · Feb 26, 2013
Salam Sangkep gan..
Langsung aja..

Ane ada kebuntuan nih..
Ane diminta bikin fitur import file dari csv ke database, nah itu buat create data karyawan.. sama update data karyawan.
Kalo untuk upload ane udah agak paham caranya, nah buat analyze sama import ini yang masih puyeng..

Langsung kodingan aja yah..
<table><form action="analyzeCSV.php" method="POST" enctype="multipart/form-data">
<tr><td><strong>Add New Staff :</strong></td></tr>

<tr><td><p>Your CSV file: </p></td><td><input type="file" name="file" id="file"></td>
<td><input type="submit" name="upload_add" value="Submit"></td></tr>
</form>
<form action="update_upload.php" method="POST" enctype="multipart/form-data">

<tr><td><strong>Update Data Staff</strong></td></tr>

<tr><td><p>Your CSV file: </p></td><td><input type="file" name="file" id="file"></td>
<td><input type="submit" name="upload_update" value="Submit"></td></tr>
</form>
</table>
Ini buat upload-nya
if($_FILES['file']['type'] != "application/vnd.ms-excel"){
	die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
	session_start();
	include('konek_db.php');
	connect_db();
	
	$findings = "
	<form method=\"post\" action=\"importCSV2.php\">
	<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">
		<tr>
			<td>Result</td>
			<td>NIK</td>
			<td>First Name</td>
			<td>Last Name</td>
			<td>Jabatan</td>
		</tr>";
	
	$handle = fopen($_FILES['file']['tmp_name'],"r");
	$data = fgetcsv($handle, 1000, ",");
	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
		$nik = mysql_real_escape_string($data[0]);
		$fname = mysql_real_escape_string($data[1]);
		$lname = mysql_real_escape_string($data[2]);
		$jab = mysql_real_escape_string($data[3]);
		
		//Check if row is in database already
		$sql = "SELECT * FROM test WHERE nik = '".$nik."'";
		$result = mysql_query($sql);
		$count = mysql_num_rows($result);
		$_SESSION['insert'] = "";
		if ($count > 0){
			$findings = $findings . "
			<tr>
				<td bgcolor=\"#FF0000\">DB Duplicate</td>
				<td>".$nik."</td>
				<td>".$fname."</td>
				<td>".$lname."</td>
				<td>".$jab."</td>
			</tr>";
		}
		elseif(strpos($_SESSION['insert'],"'".$nik."'")!== false){
			$findings = $findings."
			<tr>
				<td bgcolor=\"#FF0000\">File Duplicate</td>
				<td>".$nik."</td>
				<td>".$fname."</td>
				<td>".$lname."</td>
				<td>".$jab."</td>
			</tr>";
		}
		else{
			$_SESSION['insert'] = $_SESSION['insert']. "INSERT INTO test (nik,fname,lname,jabatan) VALUES ('".$nik."','".$fname."','".$lname."','".$jab."')";
			$findings = $findings."
			<tr>
				<td bgcolor=\"#00FF00\">&nbsp;</td>
				<td>".$nik."</td>
				<td>".$fname."</td>
				<td>".$lname."</td>
				<td>".$jab."</td>
			</tr>";
			
		}
	}
	
	
	$findings = $findings."
	<tr>
		<td colspan=\"5\"><div align=\"center\"><input type=\"submit\" value=\"Confirm\"/></div></td>
	</tr>
	</table>
	</form>";
		echo $findings;
}
else{
	die("You shouldn't be here");
}
Jujur ini sebetulnya dapet dari situs luar, cuma ane modifikasi dikit..
ini buat analyze-nya.
session_start();

if(!isset($_SESSION['insert'])){
	die("You shouldn't be here");
}

include('konek_db.php');
connect_db();

$queries = explode(',', $_SESSION['insert']);

foreach($queries as $query){
	if($query != ""){
		echo $query;die;
		mysql_query($query);
	}
}
echo "Done";

ini buat importnya..
Nah di pas analyze itu normal, ada record yang redundant g, pas di confirm, tulisannya "Done", tapi record yang ke Insert cuma 1.

Ane udah rubah dari ";" jadi"," tp g ngaruh malah g jalan.

Gimana nih gan,mohon pencerahannya..
Terima kasih sebelumnya..
Silahkan login untuk menjawab!
0
Loading...
Ellyx Christian · Feb 28, 2013 · 0 Suka · 0 Tidak Suka
biar lebih mudah (dan juga lebih tepat) $_SESSION itu diisi dengan array bukan string, seperti:
if($_FILES['file']['type'] != "application/vnd.ms-excel"){
	die("This is not a CSV file.");
}
elseif(is_uploaded_file($_FILES['file']['tmp_name'])){
	session_start();
	include('konek_db.php');
	connect_db();
 
	$findings = "
	<form method=\"post\" action=\"importCSV2.php\">
	<table width=\"100%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">
		<tr>
			<td>Result</td>
			<td>NIK</td>
			<td>First Name</td>
			<td>Last Name</td>
			<td>Jabatan</td>
		</tr>";
 
	$handle = fopen($_FILES['file']['tmp_name'],"r");
	$data = fgetcsv($handle, 1000, ",");
	$_SESSION['insert'] = array();
	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){
		$nik = mysql_real_escape_string($data[0]);
		$fname = mysql_real_escape_string($data[1]);
		$lname = mysql_real_escape_string($data[2]);
		$jab = mysql_real_escape_string($data[3]);
 
		//Check if row is in database already
		$sql = "SELECT * FROM test WHERE nik = '".$nik."'";
		$result = mysql_query($sql);
		$count = mysql_num_rows($result);
		if ($count > 0){
			$findings = $findings . "
			<tr>
				<td bgcolor=\"#FF0000\">DB Duplicate</td>
				<td>".$nik."</td>
				<td>".$fname."</td>
				<td>".$lname."</td>
				<td>".$jab."</td>
			</tr>";
		}
		elseif(isset($_SESSION['insert'][$nik])){
			$findings = $findings."
			<tr>
				<td bgcolor=\"#FF0000\">File Duplicate</td>
				<td>".$nik."</td>
				<td>".$fname."</td>
				<td>".$lname."</td>
				<td>".$jab."</td>
			</tr>";
		}
		else{
			$_SESSION['insert'][$nik] = "INSERT INTO test (nik,fname,lname,jabatan) VALUES ('".$nik."','".$fname."','".$lname."','".$jab."')";
			$findings = $findings."
			<tr>
				<td bgcolor=\"#00FF00\">&nbsp;</td>
				<td>".$nik."</td>
				<td>".$fname."</td>
				<td>".$lname."</td>
				<td>".$jab."</td>
			</tr>";
 
		}
	}
 
 
	$findings = $findings."
	<tr>
		<td colspan=\"5\"><div align=\"center\"><input type=\"submit\" value=\"Confirm\"/></div></td>
	</tr>
	</table>
	</form>";
		echo $findings;
}
else{
	die("You shouldn't be here");
}
dan untuk simpannya:
session_start();
 
if(!isset($_SESSION['insert'])){
	die("You shouldn't be here");
}
 
include('konek_db.php');
connect_db();
 
$queries = $_SESSION['insert'];
 
foreach($queries as $query){
	if($query != ""){
		mysql_query($query);
	}
}
echo "Done";
0
Loading...
Rintaka Jayadi Brata · Apr 2, 2013 · 0 Suka · 0 Tidak Suka
Sip, terima kasih banyak suhu...