0

solved: i missed ; in echo file

I am new to php, i have succesfully loaded data into sql using input textbox in html but i am unable to load the same into database by reading csv file and i am getting parse error for the fopen function. pls help

upload.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "ib";
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } else
        $file=$_FILES["file"]["tmp_name"];

    echo $file
    fopen($file, 'r');
    while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
         $sql = "INSERT into ib(atmid,location,zone,cash,fault) values('.$emapData[0]','.$emapData[1]','.$emapData[2]','.$emapData[3]','.$emapData[4])";
        $conn->query($sql);
    }
    fclose($test);
    echo "CSV File has been successfully Imported.";
?>
  • Could you publish the parse error echo on the fopen line please ? – Jean-Luc Aubert Aug 21 '17 at 13:02
  • Just remove the unnecessary concatenation so `$sql = "INSERT into ib(atmid,location,zone,cash,fault) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]')";` – RiggsFolly Aug 21 '17 at 13:03
  • ) Parse error: syntax error, unexpected '$abu' (T_VARIABLE), expecting ',' or ';' in C:\wamp64\www\ib\upload.php on line 19 – Mohamed Abubakkar Aug 21 '17 at 13:24

1 Answers1

2

You $sql has quotes problem, below is the updated one:

 $sql = "INSERT into ib(atmid,location,zone,cash,fault) values('".$emapData[0] ."','".$emapData[1] ."','" . $emapData[2] . "','".$emapData[3] . "','".$emapData[4] . "')";
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
  • 1
    Just removing the unnecessary `.` concatenation to `$sql = "INSERT into ib(atmid,location,zone,cash,fault) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapDa‌​ta[3]','$emapData[4]‌​')";` makes it so much easier to read, and maintain – RiggsFolly Aug 21 '17 at 13:07
  • brother still the same error occurs, i have changed as you have mentioned. – Mohamed Abubakkar Aug 21 '17 at 13:18
  • Try `echo $sql` and see if that SQL works in your MySQL – Milan Chheda Aug 21 '17 at 13:53