Php UNDEFINED INDEX on csv file importer to mysql through php array

  data-import, mysql, php, string

I am importing rims data from csv file to mysql table through php import function.
I have the database with following:

public function up()
    {
        Schema::create('Rims', function (Blueprint $table) {
            $table->increments('rimId');
            $table->string('rimItemNumber')->nullable();
            $table->string('rimDescription')->nullable();
            $table->string('season')->nullable();
            $table->integer('active')->nullable();
            $table->string('supplier')->nullable();
            $table->string('brand')->nullable();
            $table->string('name')->nullable();
            $table->string('size')->nullable();
            $table->string('width')->nullable();
            $table->string('pcd')->nullable();
            $table->string('et')->nullable();
            $table->string('loadIndex')->nullable();
            $table->string('centerHole')->nullable();
            $table->string('color')->nullable();
            $table->string('rimDesign')->nullable();
            $table->string('image')->nullable();
            $table->integer('stock')->nullable();
            $table->decimal('costPrice')->nullable();
            $table->decimal('retailPrice')->nullable();
            $table->decimal('price')->nullable();
            $table->string('deliveryTime')->nullable();
            $table->string('allowedOnCars')->nullable();
            $table->string('maxLoad')->nullable();
            $table->timestamps();
        });

And in my importer function i have the following:

// Reading file
$file = fopen($filepath,"r");
// extract the field names from the first row of your csv
$keys = fgetcsv($file, 1000, ';');

$importData_arr = [];
while ($line = fgetcsv($file, 1000, ';')) {
    $data = array_combine($keys, $line);
    // Do something with the data
    var_dump($data);
    $data['rimItemNumber'] = str_replace('’', '', $data['rimItemNumber']);
    $data['rimDescription'];
    $data['season'];
    $data['active'];
    $data['supplier'];
    $data['brand'];
    $data['name'];
    $data['size'];
    $data['width'];
    $data['pcd'];
    $data['et'];
    $data['loadIndex'];
    $data['centerHole'];
    $data['color'];
    $data['rimDesign'];
    $data['image'];
    $data['stock'];
    $data['costPrice'];
    $data['retailPrice'];
    $data['price'];
    $data['deliveryTime'];
    $data['allowedOnCars'];
    $data['maxLoad'];
    $importData_arr[] = $data;
}
fclose($file);

dump($importData_arr);
// Insert to MySQL database
foreach($importData_arr as $importData){
// var_dump($importData);
    $importData = array(
    $myRimItemNumber = $importData['rimItemNumber'],
    $myRimDescription = $importData['rimDescription'],
   $mySeason = $importData['season'],
   $myActive = $importData['active'],
   $mySupplier = $importData['supplier'],
   $myBrand = $importData['brand'],
   $myName = $importData['name'],
   $mySize = $importData['size'],
   $myWidth = $importData['width'],
   $myPcd = $importData['pcd'],
   $myEt = $importData['et'],
   $myLoadIndex = $importData['loadIndex'],
   $myCenterHole = $importData['centerHole'],
   $myColor = $importData['color'],
   $myRimDesign = $importData['rimDesign'],
   $myImage = $importData['image'],
   $myStock = $importData['stock'],
   $myCostPrice = $importData['costPrice'],
   $myRetailPrice = $importData['retailPrice'],
   $myPrice = $importData['price'],
   $myDeliveryTime = $importData['deliveryTime'],
   $myAllowedOnCars = $importData['allowedOnCars'],
   $myMaxLoad = $importData['maxLoad']);
RimsUpload::insertData($importData);

My insertData function:

public static function insertData($data){

      $value=DB::table('Rims')->where('rimItemNumber', $data['rimItemNumber'])->get();
      if($value->count() == 0){
         DB::table('Rims')->insert($data);
      }
   }

I did a var_dump on my $data and get the following:

array(23) { ["rimItemNumber"]=> string(21) "1565410030KR2’19BKL" ["rimDescription"]=> string(6) "foobar" ["season"]=> string(3) "foo" ["active"]=> string(3) "foo" ["supplier"]=> string(3) "foo" ["brand"]=> string(5) "CADES" ["name"]=> string(4) "Eros" ["size"]=> string(2) "15" ["width"]=> string(3) "foo" ["pcd"]=> string(5) "4-100" ["et"]=> string(2) "30" ["loadIndex"]=> string(3) "foo" ["centerHole"]=> string(4) "73.1" ["color"]=> string(5) "Black" ["rimDesign"]=> string(3) "foo" ["image"]=> string(18) "/images/fælg1.jpg" ["stock"]=> string(2) "26" ["costPrice"]=> string(3) "666" ["retailPrice"]=> string(3) "666" ["price"]=> string(4) "1699" ["deliveryTime"]=> string(3) "foo" ["allowedOnCars"]=> string(3) "foo" ["maxLoad"]=> string(3) "foo" }


And everything seems correct?!
Can anyone see why i am getting an undefined index error on "rimItemNumber".
The whole error message received:

Undefined index: rimItemNumber on line : "$value=DB::table('Rims')->where('rimItemNumber', $data['rimItemNumber'])->get();" AND "RimsUpload::insertData($importData);"

Source: Ask PHP

LEAVE A COMMENT