mysqli插入语句不与phpexcel一起使用

时间:2013-10-20 09:00:58

标签: php mysqli

我正在尝试通过网页将excel 2007文件中的数据插入到mysql中。我正在使用phpexcel库而且它无法正常工作。请帮助..我的代码看起来像这样: 我已经检查了每个地方,大多数代码都是这样的;因为我的数据库仍然是空的

<?php  
//In case you want the script to be executed at a predefined time interval, use this   
/*header('Refresh: 30'); */

//Establishing connection to Mysql Database:
$mysqli = new mysqli("localhost", "root","","final");   
if (mysqli_connect_errno()) {    
echo mysqli_connect_error();     
exit(); } 
//PHPEXCEL Reader
            require 'includes/PHPExcel.php';
            require_once 'includes/PHPExcel/IOFactory.php';
//action that happens when the button is pressed
if (isset($_POST['button1'])) 
        {
                echo "button on is pressed";
                 if ($_FILES["file"]["error"] > 0)
                {
                        echo "Error: " . $_FILES["file"]["error"] . 
            "You have not selected a file or some other error <br />";
                }
                else
                {       //  Errorless  start 
                        $inputFileName=$_FILES["file"]["name"];
                        $objPHPExcel = PHPExcel_IOFactory::load("C:/julisha/uploads/".$inputFileName);   

                                        // Creating a temporary copy on the server 
                                        $location="C:/wamp/www/julisha/uploads/"; // write the location on 
                    // server where a copy should be created
                                        move_uploaded_file($_FILES["file"]["tmp_name"],
                    $location . $_FILES["file"]["name"]);
//$inputFileName    = $file_name;
$inputFileType  = 'Excel2007'; 

$objReader  = PHPExcel_IOFactory::createReader("$inputFileType");
$objReader->setReadDataOnly(true);

$objWorksheet   = $objPHPExcel->getActiveSheet();

$highestRow     = $objWorksheet->getHighestRow();
$highestColumn  = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//Display Excel data on webpage
echo '<table>' . "\n";
for ($row = 1; $row <= $highestRow; ++$row) {
    echo '<tr>' . "\n";

for ($col = 0; $col <= $highestColumnIndex; ++$col) {
    echo '<td>' . $objWorksheet->getCellByColumnAndRow($col, $row)->getValue() . '</td>' . "\n";
}
echo '</tr>' . "\n";    
}
echo '</table>' . "\n";
//Preparing to import data from Excel to Mysql Database:
for($row = 2; $row <= $highestRow; ++$row) {
for($col = 0; $col < $highestColumnIndex; ++$col) {     
$rows[$col] = $objWorksheet->getCellByColumnAndRow($col, $row);

}


$mysqli->query("INSERT INTO temp_results (Adm_No,Engl,Kis,Math,Chem) VALUES ('$rows[0]', '$rows[1]', '$rows[2]', '$rows[3]', '$rows[4]',)");

  }

                }            
}
$mysqli->close();
?>

请帮助

1 个答案:

答案 0 :(得分:0)

查询末尾有,请将其删除

变化

 $mysqli->query("INSERT INTO temp_results (Adm_No,Engl,Kis,Math,Chem) VALUES ('$rows[0]', '$rows[1]', '$rows[2]', '$rows[3]', '$rows[4]',)");

$mysqli->query("INSERT INTO temp_results (Adm_No,Engl,Kis,Math,Chem) VALUES ('$rows[0]', '$rows[1]', '$rows[2]', '$rows[3]', '$rows[4]')");