An error occurred while importing excel to mysql? MySQL Excel SQL phpexcel
// 1.html Upload page
// Upload. php execution program file
If ($ _ POST ['leadexcel '] = "true ")
{
$ Filename = $ _ FILES ['input'] ['name'];
$ Tmp_name = $ _ FILES ['input'] ['tmp _ name'];
$ Msg = uploadFile ($ filename, $ tmp_name );
Echo $ msg;
}
// Import an Excel file
Function uploadFile ($ file, $ filetempname)
{
// Set the path for storing uploaded files
$ FilePath = 'upfile /';
$ Str = "";
// The following path is modified according to your PHPExcel path.
Require_once 'phpexcel/PHPExcel. php ';
Require_once 'phpexcel/PHPExcel/IOFactory. php ';
Require_once 'phpexcel/PHPExcel/Reader/excel5.php ';
// Set the time zone.
$ Time = date ("y-m-d-H-I-s"); // The current Upload time.
// Obtain the extension of the uploaded file
$ Extend = strrchr ($ file ,'.');
// Name of the uploaded file
$ Name = $ time. $ extend;
$ Uploadfile = $ filePath. $ name; // address of the uploaded file name
// The move_uploaded_file () function moves the uploaded file to a new location. If yes, true is returned. otherwise, false is returned.
$ Result = move_uploaded_file ($ filetempname, $ uploadfile); // if the file is uploaded to the current directory
// Echo $ result;
If ($ result) // if the file is uploaded successfully, import the file to excel.
{
Include "conn. php ";
$ ObjReader = PHPExcel_IOFactory: createReader ('excel5'); // use excel2007 for 2007 format
$ ObjPHPExcel = $ objReader-> load ($ uploadfile );
$ Sheet = $ objPHPExcel-> getSheet (0 );
$ HighestRow = $ sheet-> getHighestRow (); // gets the total number of rows.
$ HighestColumn = $ sheet-> getHighestColumn (); // gets the total number of columns
$ ObjWorksheet = $ objPHPExcel-> getActiveSheet ();
$ HighestRow = $ objWorksheet-> getHighestRow ();
Echo 'hehestrow = '. $ highestRow;
Echo"
";
$ HighestColumn = $ objWorksheet-> getHighestColumn ();
$ HighestColumnIndex = PHPExcel_Cell: columnIndexFromString ($ highestColumn); // The total number of columns
Echo 'hehestcolumnindex = '. $ highestColumnIndex;
Echo"
";
$ Headtitle = array ();
For ($ row = 1; $ row <= $ highestRow; $ row ++)
{
$ Strs = array ();
// Note that the index of the highestColumnIndex column starts from 0.
For ($ col = 0; $ col <$ highestColumnIndex; $ col ++)
{
$ Strs [$ col] = $ objWorksheet-> getCellByColumnAndRow ($ col, $ row)-> getValue ();
}
$ SQL = "INSERT INTO test_list ('num', 'sex') VALUES (
'{$ Strs [0]}',
'{$ Strs [1]}'
)";
// Die ($ SQL );
If (! Mysql_query ($ SQL ))
{
Return false;
Echo 'SQL statement error ';
}
}
}
Else
{
$ Msg = "import failed! ";
}
Return $ msg;
}
?>
Import failed. please take a look! Thank you! In a hurry.
Reply to discussion (solution)
If echo $ msg; the output import fails!
It indicates that there is a problem with this code.
// Set the path for storing uploaded files
$ FilePath = 'upfile /';
$ Str = "";
// The following path is modified according to your PHPExcel path.
Require_once 'phpexcel/PHPExcel. php ';
Require_once 'phpexcel/PHPExcel/IOFactory. php ';
Require_once 'phpexcel/PHPExcel/Reader/excel5.php ';
// Set the time zone.
$ Time = date ("y-m-d-H-I-s"); // The current Upload time.
// Obtain the extension of the uploaded file
$ Extend = strrchr ($ file ,'.');
// Name of the uploaded file
$ Name = $ time. $ extend;
$ Uploadfile = $ filePath. $ name; // address of the uploaded file name
// The move_uploaded_file () function moves the uploaded file to a new location. If yes, true is returned. otherwise, false is returned.
$ Result = move_uploaded_file ($ filetempname, $ uploadfile); // if the file is uploaded to the current directory
// Echo $ result;
Troubleshoot by line
I don't know where an error occurred. can't I find it?
~ Help me ~ Grateful...
Step-by-step breakpoint debugging.