我想使用PHP将我的excel文件保存为数据库中的表。可能吗? 我尝试过如下。但我们需要提供表名。
<html>
<head>
<?php
if(isset($_POST["Import"]))
{
include 'database.php';
$filename=$_FILES["file"]["tmp_name"];
$heading=true;
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000)) !== FALSE)
{
$num = count($emapData);
echo $num;
echo $emapData[0];
//check if the heading row
if($heading) {
// unset the heading flag
$heading = false;
// skip the loop
continue;
}
$sql = "INSERT into sheet(name,first_name,last_name) values('$emapData[0]','$emapData[1]','$emapData[2]')";
$row=mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
</head>
<body>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">
Only Excel/CSV File Import.
</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
</body>
</html>