我无法使用php和mysql将数据从excel上传到数据库。我的代码如下。
使用这些代码添加了一些不需要的数据。任何人都可以为我解决这些问题吗?
HTML:
<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>
PHP
<?php
if(isset($_POST["Import"]))
{
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= '';
$db= 'excel'; // Database Name.
$conn=@mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
$count = 0;
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
/*print_r($emapData);
exit();*/
$count++;
if($count>1)
{
echo $in="insert into first (name, email) values('$emapData[0]','$emapData[1]')";
$insertTable= mysql_query($in);
}
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
{
echo 'Invalid File:Please Upload CSV File';
}
}
?>