Hi,
Below code is working for uploading and insertion in database.
How below can be done without Without Reloading the Page.
<?php
session_start();
$userId=3; //stored userid of current login user from session
include("../db.php"); //include db part
if (isset($_FILES["csv"])) {
$uploadedFile = $_FILES["csv"];
$uploadedFileName = $uploadedFile ["name"]; //name of uploaded file
$getExtnOfUploadedFile = pathinfo($uploadedFileName , PATHINFO_EXTENSION); //get extension of uploaded file using pathinfo function
if ($getExtnOfUploadedFile !="CSV" && $getExtnOfUploadedFile !="csv") { //validate uploaded file
die('The file must be csv format.');
}
$savetoPathAndNameOfFile = "upload/".time()."_".$userId.".".$getExtnOfUploadedFile ; //rename uploaded file
move_uploaded_file($uploadedFile ["tmp_name"], $savetoPathAndNameOfFile );// move uploaded file to upload folder
$handle = fopen($savetoPathAndNameOfFile , "r") or die('Unable to open uploaded file!", "inventory'); // Open the file for reading
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO csv_import(keywords,tags) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
echo "uploaded Sucessfully !!!";
}
else{
echo"Please upload CSV file or try again";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Insertion of CSV Data In to db</title>
<script type="text/javascript" src="scripts/jquery-1.8.2.min.js"></script>
</head>
<body>
<div id="" class="">
<form action="import.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Import" />
</form>
</div>
</body>
</html>