Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

mysql - Undefined offset error on php when importing a CSV

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();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sometimes you need to ignore the last line or lines of the csv. I set the number of ignore lines from the top as $start_offset and the number of lines to ignore from the bottom as $end_offset. Start with zero and increase until the offset error goes away
Here's how I do it:

$data = file_get_contents($filename);//load up csv

$data_array = explode("
", $data);//break file into lines
$csv = array_map('str_getcsv', $data_array);//break up comma delimited
$csv_len = count($csv); //count of number of lines
$start_offset = 2;
$end_offset = 3;
for ($i=$start_offset; $i<$csv_len-$end_offset; $i++)
{
    //access columns as $csv[$i][0], $csv[$i][1] etc
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...