I am trying to import a CSV file into my database and it is being successfully executed at the end. But, during the execution, I receive an "undefined offset" error message and when I checked the data imported, I see that there are some null records updated in the table. How can I avert importing these null cells into my database? I also would like not to see these error messages.
    <?php
require_once("database.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
/// Delete table contents
$dsql = "TRUNCATE TABLE User_Mirror_Tbl";
if ($conn->query($dsql) === TRUE) {
  echo "Table content is truncated successfully". PHP_EOL;
  } else {
  echo "Error: " . $dsql . "<br>" . $conn->error;
  }
//read file
$csvfile=file_get_contents("/samba/import/User_Update_Tbl.csv");
//counters:
$record_number=0;
$record_number_err=0;
$lines = explode(PHP_EOL, $csvfile);
$array = array();
foreach ($lines as $line) {
    $field = str_getcsv($line);
      if $field[0] != ''){
$field[1]= ( $field[1] == '' ? NULL : $field[1]);
$field[6]= ( $field[6] == '' ? NULL : $field[6]);
$field[7]= ( $field[7] == '' ? NULL : $field[7]);
        $sql="INSERT INTO User_Mirror_Tbl (History_Record_ID, Employee_ID, Application_ID, User_Status, Record_Date, User_Name, User_Role, Last_Signon, UserKeyString)
                VALUES
                ('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]') ";
        //insert record to database
        if ($conn->query($sql) === TRUE) {
        //      echo "New record created successfully". PHP_EOL;
                $record_number=$record_number+1;
        } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
                $record_number_err=$record_number_err+1;
        }
}
}
echo $record_number.' Successful record and '.$record_number_err.' Unsuccessful record executed.';
$conn->close();
<?php
require_once("database.php");
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
/// Delete table contents
$dsql = "TRUNCATE TABLE User_Mirror_Tbl";
if ($conn->query($dsql) === TRUE) {
  echo "Table content is truncated successfully". PHP_EOL;
  } else {
  echo "Error: " . $dsql . "<br>" . $conn->error;
  }
//read file
$csvfile=file_get_contents("/samba/import/User_Update_Tbl.csv");
//counters:
$record_number=0;
$record_number_err=0;
$lines = explode(PHP_EOL, $csvfile);
$array = array();
foreach ($lines as $line) {
    $field = str_getcsv($line);
      if $field[0] != ''){
$field[1]= ( $field[1] == '' ? NULL : $field[1]);
$field[6]= ( $field[6] == '' ? NULL : $field[6]);
$field[7]= ( $field[7] == '' ? NULL : $field[7]);
        $sql="INSERT INTO User_Mirror_Tbl (History_Record_ID, Employee_ID, Application_ID, User_Status, Record_Date, User_Name, User_Role, Last_Signon, UserKeyString)
                VALUES
                ('$field[0]','$field[1]','$field[2]','$field[3]','$field[4]','$field[5]','$field[6]','$field[7]','$field[8]') ";
        //insert record to database
        if ($conn->query($sql) === TRUE) {
        //      echo "New record created successfully". PHP_EOL;
                $record_number=$record_number+1;
        } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
                $record_number_err=$record_number_err+1;
        }
}
}
echo $record_number.' Successful record and '.$record_number_err.' Unsuccessful record executed.';
$conn->close();
 
    