1

I have the following CSV file without any headers as in text wrangler

54000,Khalid Plaza,Sunrise Towers,Airsure Premium,500
54001,Genie  Villa,Dubai Land,Airsure Standard,478
435678,Vox Plaza,Sunset Towers,Airsure Premium,500
22332,imax Plaza,Moonrise Towers,Airsure Premium,500

I and trying to upload this using the following php code

if($_FILES["file"]["size"] > 0)
         {
            $file = fopen($filename, "r");
            while (($getData = fgetcsv($file)) !== FALSE)
             {
                $sql = "INSERT into jobtktdesc (jobtktnum,custname,sitename,typecont,idcontract) 
                   values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."')";
                   $result = mysqli_query($conn, $sql);
                if(!isset($result))
                {
                    echo "<script type=\"text/javascript\">
                            alert(\"Invalid File:Please Upload CSV File.\");
                            window.location = \"index.php\"
                          </script>";       
                }
                else {

                      echo "<script type=\"text/javascript\">
                        alert(\"Submitted CSV File.\");
                        window.location = \"index.php\"
                    </script>";
                }
             }

             fclose($file); 
         }

However I am able to upload only the first line. All other lines are skipped.

Regards,

Arjun Bhandari
  • 191
  • 1
  • 11
  • 1
    http://stackoverflow.com/a/41294508/267540 – e4c5 Jan 11 '17 at 07:20
  • I looked at the post above and get the following error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Plaza,Sunrise Towers,Airsure Premium,500 54001,Genie Villa,Dubai Land,Airsure S' at line 1; new code is while (($line = fgets($file)) !== FALSE) { $values .= "($line),"; $values = substr($values, 0, strlen($values) - 1); $conn->query($sql_prefix . $values) or die($conn->error); } – Arjun Bhandari Jan 11 '17 at 08:28
  • Please try to read that comment. Can you? Please edit the question. – e4c5 Jan 11 '17 at 08:32

0 Answers0