Jump to content

Skip first line on import csv using php mysqli


samuel_lopez

Recommended Posts

hi. I have a problem on importing csv file using php.
Below code is working but its inserting the first line(header).
I don't need to import also headers.How can I prevent the code to save headers.
Your response is much appreciated.Thank you
 

<?php
include 'config.php';
if(isset($_POST["import"])){
		echo $filename=$_FILES["file"]["tmp_name"];
		 if($_FILES["file"]["size"] > 0)
		 {
		  	$file = fopen($filename, "r");
	         while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
	         {
	         
	         $sql = $mysqli->query("INSERT INTO table (name,age) 
	         VALUES ('$emapData[1]','$emapData[2]')")
	         		or die(mysqli_error($mysqli));
	         
				if(!$sql)
				{
					echo "<script type=\"text/javascript\">
							alert(\"Invalid File:Please Upload CSV File.\");
							window.location = \"import_student.php\"
						</script>";
				}
	         }
	         fclose($file);
	         
	         echo "<script type=\"text/javascript\">
						alert(\"CSV File has been successfully Imported.\");
						window.location = \"import_student.php\"
					</script>";
			 
			mysqli_close($mysqli); 
				
		 	
			
		 }
	}	 
?>		 

Link to comment
Share on other sites


$row = 1;
if (($handle = fopen("Your_File.csv", "r")) !== FALSE) {
    fgetcsv($handle, 10000, ",");
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
Edited by benanamen
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.