PHP脚本不将Excel文件导入SQL数据库

时间:2018-01-17 04:20:30

标签: php mysql mysqli

有关我的服务器的一些信息:

Server: Localhost via UNIX socket
Server type: MySQL
Server version: 5.7.20-0ubuntu0.16.04.1 - (Ubuntu)
Protocol version: 10
User: 
Server charset: UTF-8 Unicode (utf8)

Apache/2.4.18 (Ubuntu)
Database client version: libmysql - mysqlnd 5.0.11-dev - 20120503 - $Id: 76b08b24596e12d4553bd41fc93cccd5bac2fe7a $
PHP extension: mysqli Documentation
PHP version: 5.6.33-1+ubuntu16.04.1+deb.sury.org+1

我正在尝试运行一个脚本,它将完成两件事:在我的excel .xls文件中读取数据,将该数据导入特定的数据库表。我的网站没有导入xls文件的功能,所以我使用单独的脚本来完成它。

这是我的代码:

 <html>
   <head> 
  <title>Save Excel file details to the database</title>
  </head>
  <body>
<?php
 error_reporting(E_ALL); ini_set('display_errors', '1');
    $servername = "localhost";
 $username = "";
 $password = "";

 // Create connection
 $conn = new mysqli($servername, $username, $password);

    include 'reader.php';
    $excel = new Spreadsheet_Excel_Reader();
?>
    <table border="1">
    <?php
        $excel->read('collegec_database_digitalocean.xls');    
        $x=2;
        while($x<=$excel->sheets[0]['numRows']) {
            $id = isset($excel->sheets[0]['cells'][$x][1]) ? $excel->sheets[0]['cells'][$x][1] : '';
            $name = isset($excel->sheets[0]['cells'][$x][2]) ? $excel->sheets[0]['cells'][$x][2] : '';
            $describtion = isset($excel->sheets[0]['cells'][$x][3]) ? $excel->sheets[0]['cells'][$x][3] : '';
            $email = isset($excel->sheets[0]['cells'][$x][4]) ? $excel->sheets[0]['cells'][$x][4] : '';
            $phone = isset($excel->sheets[0]['cells'][$x][5]) ? $excel->sheets[0]['cells'][$x][5] : '';
            $website = isset($excel->sheets[0]['cells'][$x][6]) ? $excel->sheets[0]['cells'][$x][6] : '';
            $location = isset($excel->sheets[0]['cells'][$x][7]) ? $excel->sheets[0]['cells'][$x][7] : '';
            $postgraduatecourses = isset($excel->sheets[0]['cells'][$x][8]) ? $excel->sheets[0]['cells'][$x][8] : '';
            $shortcoursesandcertifications = isset($excel->sheets[0]['cells'][$x][9]) ? $excel->sheets[0]['cells'][$x][9] : '';
            $technicalandvocationalcourses = isset($excel->sheets[0]['cells'][$x][10]) ? $excel->sheets[0]['cells'][$x][10] : '';
            $fees = isset($excel->sheets[0]['cells'][$x][2]) ? $excel->sheets[0]['cells'][$x][11] : '';
            $undergraduatecourses = isset($excel->sheets[0]['cells'][$x][12]) ? $excel->sheets[0]['cells'][$x][12] : '';

            // Save details
            $sql_insert="INSERT INTO collegec_SchoolDir (id,name,describtion,email,phone,website,location,postgraduatecourses,shortcoursesandcertifications,technicalandvocationalcourses,fees,undergraduatecourses) VALUES ('','$name','$describtion','$email','$phone','$website','$location','$postgraduatecourses','$shortcoursesandcertifications','$technicalandvocationalcourses','$fees','$undergraduatecourses')";

            if (mysqli_query($conn, $sql_insert)) {
                echo "New record created successfully";
 } else {
        echo "Error: " . $sql_insert . "<br>" . mysqli_error($conn);
 }


          $x++;
        }
 mysqli_close($conn);
    ?>    
</table>

</body>
 </html>

未显示任何错误。只是一个空白的白色屏幕。我检查了数据库,没有导入新数据。我还仔细检查了excel文件,它存在于服务器上并且包含数据。也许导入文件中的上述代码有错误?

0 个答案:

没有答案