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> 

You are going to need to include javascript in your code and leverage Ajax to upload the file behind the scenes.

Here is a tutorial I quickly found doing a search... Uploading Files with Ajax. This tutorial is not taking the file and uploading data to the DB, but you already have that part of the code. The part of the tutorial that should interest you is how ajax was used to upload the file. Once the file reaches the server, use your code to check to make sure its a .csv file and then do your part to insert the data in the DB.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.