An error occurred while importing excel to mysql?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.