Hi,

I am using below PHP script for insertion in mysql table from CSV file.
Below code is working fine but i am trying to leave first row of CSV as it contains heading
for data.
need suggestions as it is reading from very first row.

 <?php 
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("test",$connect); //select the table
//

if ($_FILES[csv][size] > 0) {

    //get the csv file
   //echo $_FILES[csv][size];
   $file = $_FILES[csv][tmp_name];
    $handle = fopen($file,"r");

    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
             $data[0];
             $data[1];
             $data[2];
             $data[3];

              mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email ,contact_qual) VALUES
                (
                    '".addslashes($data[0])."',
                    '".addslashes($data[1])."',
                    '".addslashes($data[2])."',
                    '".addslashes($data[3])."'
                )
            ");
        }
    } while ($data = fgetcsv($handle,1000,",","'"));
    //

    //redirect
    header('Location: import.php?success=1'); die;

}

?>

<!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>Import a CSV File with PHP & MySQL</title>
</head>

<body>

<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" 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="Submit" />
</form>

</body>
</html>

Recommended Answers

All 2 Replies

Member Avatar for LastMitch

@jacob21

read csv from second row & insert in to mysql

You need to INSERT a valve so it read from the row.

The query should look like this:

INSERT INTO 'yourdb'.'table' ('id','contents' ) VALUES ('1', '1');

I would use a while loop instead with a switch that would allow you to specify whether you want to include the first row.

<?php
    $countFirstRow = false;

    while ($data = fgetcsv($handle,1000,",","'")) {
        if (!$countFirstRow) {
            // go back to the start of the loop to the next line.
            continue;
        }

        if ($data[0]) {
            // removed your $data[0]...[3]
            // as they were not doing anything here.

            mysql_query("INSERT INTO contacts (contact_first, contact_last, contact_email ,contact_qual) VALUES
            (
            '".addslashes($data[0])."',
            '".addslashes($data[1])."',
            '".addslashes($data[2])."',
            '".addslashes($data[3])."'
            )
            ");
        }
    }
?>
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.