简体   繁体   English

通过php将Excel工作表数据存储到数据库中

[英]Store excel sheet data into database through php

I got success to show the excel sheet data on my php page in table form by some php files from http://www.phpclasses.org/browse/package/6279/download/zip.html and I am trying to store text of each columns of table into database through below code but I am getting Notice: Undefined index: feed10 我成功地通过http://www.phpclasses.org/browse/package/6279/download/zip.html上的一些php文件以表格形式在我的php页面上显示了excel表格数据,并且我试图存储的文本通过下面的代码将表的每一列插入数据库,但我得到注意:未定义索引:feed10

Code is 代码是

<html>
<head>
<title>Upload</title>
</head>
<body>
<form  method="post" enctype="multipart/form-data" >
Upload file (*.XLSX file) <input type="file" name="file" value=""  /><input type="submit" value="Upload" />
</form>
<form  method="post" enctype="multipart/form-data" action="loadPHP.php">
<?php

if (isset($_FILES['file'])) {

require_once "simplexlsx.class.php";

$row = 0;

$xlsx = new SimpleXLSX( $_FILES['file']['tmp_name'] );

echo '<h2>Uploaded Data</h2>';
echo '<table >';

list($cols,) = $xlsx->dimension();

foreach( $xlsx->rows() as $k => $r) {
//  if ($k == 0) continue; // skip first row
echo '<tr>';
for( $i = 0; $i < $cols; $i++) {
echo '<td name="feed'.$row.''.$i.'">'.( (isset($r[$i])) ? $r[$i] : '&nbsp;' ).'</td>';

}
echo '</tr>';
$row++;
}
echo '</table>';
echo '<input type="submit" value="Load in Database"/>';
}

?>  
</form>

</div>
</body>
</html>

loadPHP.php loadPHP.php

<?php
    session_start();
    $rw=1;
    $result='';
    $con=mysql_connect("localhost","root","12345");
    if(!$con) {
        die('could not connect'.mysql_error());
    }
    mysql_select_db("birt",$con);
    for( $col = 0; $col < 2; $col++ ){
        $day=$_POST['feed'.$rw.''.$col.''];
        $order = "INSERT INTO lecture_plan (topic ) VALUES ('$day')";
        $result = mysql_query($order);
    }
    if($result){
        header('Location: simplexlsx.example3.php');
    } else{
        echo("<br>Input data is fail");
    }
    mysql_close($con);
?>`

The data is not being submitted through the form because you are setting it directly in the td tag. 数据不是通过表单提交的,因为您是直接在td标记中设置的。

Try moving the data to a hidden input, like so: 尝试将数据移至隐藏的输入,如下所示:

echo '<td>'.( (isset($r[$i])) ? $r[$i] : '&nbsp;' ).' <input type="hidden" name="feed'.$row.''.$i.'" value="'.( (isset($r[$i])) ? $r[$i] : null ).'" /></td>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM