-5

I have CSV file want to upload it to upload it to MySQL.

My CSV file has structure the first line include refernace information like name,date and time.

starting form second line there is information.

i want to insert the first line to table named "file_information" and all line after that in table named "main"

My CSV as below

20151251652,1,BR-1-2

1,1-20151251652,0543434232,1234123,CL-1-3,DR-1-4,DA-12,MO-12,YR-2015,HR-12,MT-12,PA-1,LN-2,1,BR-1-2

2,1-20151251652,0543434232,1234123,CL-1-3,DR-1-4,DA-12,MO-12,YR-2015,HR-1,MT-34,PA-2,LN-2,1,BR-1-2

3,1-20151251652,0543434232,1234123,CL-1-3,DR-1-4,DA-12,MO-12,YR-2015,HR-12,MT-55,PA-2,LN-2,1,BR-1-2

4,1-20151251652,0543434232,1234123,CL-1-3,DR-1-4,DA-12,MO-12,YR-2015,HR-11,MT-20,PA-1,LN-2,1,BR-1-2

PHP

<?php 

//connect to the database
$connect = mysql_connect("localhost","","");
mysql_select_db("",$connect); //select the table
//

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

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

    //loop through the csv file and insert into database
    do {
        if ($data[0]) {
            mysql_query("INSERT INTO main (control, docname, mobno, fno, cl, dr, da, mo, yr, hr, mt, pa, ln, ho, br) VALUES
                (
                    '".addslashes($data[0])."',                    
                    '".addslashes($data[1])."',                    
                    '".addslashes($data[2])."',                    
                    '".addslashes($data[3])."',                    
                    '".addslashes($data[4])."',                    
                    '".addslashes($data[5])."',                    
                    '".addslashes($data[6])."',                    
                    '".addslashes($data[7])."',                    
                    '".addslashes($data[8])."',                    
                    '".addslashes($data[9])."',                    
                    '".addslashes($data[10])."',                    
                    '".addslashes($data[11])."',                    
                    '".addslashes($data[12])."',                    
                    '".addslashes($data[13])."',                    
                    '".addslashes($data[14])."'                    

                )
            ");
        }
    } 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>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Mohammed
  • 1
  • 2
  • 2
    what did you tried? where is the problem? – donald123 Jan 26 '15 at 13:01
  • icant split this lines its take all the lines to both tables – Mohammed Jan 26 '15 at 13:04
  • say it like obama: yes you can just add a "$row-identifier" and if-statement to handle it ... – donald123 Jan 26 '15 at 13:07
  • how? i tried many codes but is fail – Mohammed Jan 26 '15 at 13:08
  • **Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php).** They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). **Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement)** instead, and **use [PDO](http://us1.php.net/pdo).** – Jay Blanchard Jan 26 '15 at 13:10
  • You have to include logic saying "if this is the first line, insert into the first table else insert into the second table". – Jay Blanchard Jan 26 '15 at 13:11

1 Answers1

0

Take a look at this example:

<?php
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {

        if($row == 1) {
           echo "First Line :".print_r($data,1);
        } else {
           echo "Other Lines :".print_r($data,1);
        }

        $row++;

    }
    fclose($handle);
}
?>
donald123
  • 5,638
  • 3
  • 26
  • 23