读取Excel文件和使用PHP将数据插入MySQL数据库时出错


Error in reading Excel file & Insert data into MySQL Database using PHP

我的目的是读取excel(.xls)文件并将其存储在数据库中,并在浏览器中显示excel文件的输入。

我在读取文件时遇到问题。我已经集成了Excelfilereader.php

程序读取 excel 文件,

但在浏览器中打印其输出(excel 文件内容)时,以锯齿形方式打印数据。

例:

学生证姓名班级学校课程地址 [字段名称]

在浏览器中,打印

相同的字段,但学生ID的数据打印在名称下,姓名数据打印在下,但其余数据都按原样打印。在前两列中打印未对齐。

用于调用 Excelreader 的代码.php:

[mysql_query("insert into submit_form(parent_id,name,lab,submission,title,sampletype,file) values('','".$_POST['name']."','".$_POST['lab']."','".$_POST['submission']."','".$_POST['title']."','".$_POST['sampletype']."','".$uploadfile."')");
$insertid=mysql_insert_id();
    $data = new Spreadsheet_Excel_Reader($_FILES['uploadfile']['name']);
//echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";
$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{   
    if(count($data->sheets[$i][cells])>0) // checking sheet not empty
    {
    //echo "Sheet $i:<br /><br />Total rows in sheet $i  ".count($data->sheets[$i][cells])."<br />";
        for($j=2;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
        { 
            $html.="<tr>";
            for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
            {
                $html.="<td>";
                $html.=$data->sheets[$i][cells][$j][$k];
                $html.="</td>";
            }
            $data->sheets[$i][cells][$j][1];
            $eid = $eid = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][1]);
            $age = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][2]);
            $gender = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][3]);
            $ethnic = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][4]);
            $cancer = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][5]);
            $sample = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][6]);
            $instrument = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][7]);
            $instrumenttype = mysqli_real_escape_string($connection,$data->sheets[$i][cells][$j][8]);
            $query = "insert into submit_form(parent_id,name,lab,submission,title,patient,age,gender,ethnic,cancer,sample,instrument,instrumenttype,attach,sampletype,file) values('".$insertid."','".$_POST['name']."','".$_POST['lab']."','".$_POST['submission']."','".$_POST['title']."','".$eid."','".$age."','".$gender."','".$ethnic."','".$cancer."','".$sample."','".$instrument."','".$instrumenttype."','1','".$_POST['sampletype']."','".$uploadfile."')";
            mysqli_query($connection,$query);
            $html.="</tr>";
        }
    }][2]

您已将mysql_*调用与mysqli_*调用混合在一起。 使用mysqli_*调用。

(您也应该转义$_POST[]列。 他们很容易受到黑客的攻击。