0

Here is my exact code and im always getting an error of Undefined offset in my array

<?php 
require_once('../../../Backend/Listings_Database/Listings_Connection.php');

$Response = array();

if($_FILES["file"]["size"] > 0) {   
    $filename=$_FILES["file"]["tmp_name"];

    $file = fopen($filename, "r"); 
    while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE) {
        (name,brand,cat_id,
         store_size,location_floor_unit,phone,traffic_of_mall,
         time_of_traffic,city) 
        values('$emapData[0]','$emapData[1]','$emapData[2]',
               '$emapData[3]','$emapData[4]','$emapData[5]',
               '$emapData[6]','$emapData[7]','$emapData[8]')"; 

        //we are using mysql_query function. it returns a resource on true else False on error 

        $result = mysqli_query($sql,$Listings_Database);

        if(! $result ) { 
            $Response[] = "Fail"; 
        } else { 
            $Response[] = "Success";
        }
    } 
    fclose($file);

    $Response[] = "Success";

    mysqli_close($Listings_Database);  
} else {
    $Response[] = "Fail";
}

var_dump($Response);

print json_encode($Response);

?>

Here is my exact code and im always getting an error of Undefined offset in my array

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Apr 03 '17 at 09:45
  • You never actually load your query into `$Listings_Database` HUGE TYPO – RiggsFolly Apr 03 '17 at 09:46
  • Your query is also a complete syntactical nonsense – RiggsFolly Apr 03 '17 at 09:47
  • Also that error message also contains a line number. _Please always include the COMPLETE error message and indicate which line in your code sample is the line with the error_ – RiggsFolly Apr 03 '17 at 09:50
  • What is `$sql` and what is `$Listings_Database` I dont see anywhere where either are being set – RiggsFolly Apr 03 '17 at 09:59
  • You have **8** column names and **9** values in the `value(...)` clause. **Lay out your code in a readable way and these minor errors will become obvious** – RiggsFolly Apr 03 '17 at 10:01
  • You do not have an `INSERT` in your query – RiggsFolly Apr 03 '17 at 10:03
  • You do not set the query you write into a variable! I assume it should be `$Listings_Database = "INSERT INTO .........` – RiggsFolly Apr 03 '17 at 10:04
  • Thanks guys , i already resolve it.. but now my problem is.. the special characters is not inserting correctly – Vincent Valentine Apr 03 '17 at 10:16
  • See http://stackoverflow.com/questions/279170/utf-8-all-the-way-through – RiggsFolly Apr 03 '17 at 10:17

1 Answers1

-1

Which line shows this "getting an error of Undefined offset" error. Also take this approach:

 $name = !empty($emapData[0]) ? $emapData[0] : '';
 $brand = !empty($emapData[1]) ? $emapData[1] : '';

and so on... And then use in your query:

insert into <tableName> (name,brand,cat_id,store_size,location_floor_unit,phone,traffic_of_mall,time_of_traffic,city) values('$name','$brand',......)";

Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14