让我们说,我想从PHP导入/上传excel文件到mysql
我的HTML如下所示
<form enctype="multipart/form-data" method="post" role="form" action="do.php">
<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"]))
{
//First we need to make a connection with the database
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= '123';
$db= 'dev'; // 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");
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$sql = "INSERT INTO `prayer` (`id` ,`country` ,`city` ,`day` ,`no` ,`datem` ,`fajer` ,`shrok` ,`dohor` ,`aser` ,`maghreb` ,`eshaa`) values ('','ksa','riyadh','$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]','$emapData[8]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
//header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
我想将值导入到我的表中。它导入一行中的每一行'$emapData[0]'
请帮助我如何解决它或给我任何资源。
编辑: -
我在Array ( [0] => 1;28/6;03:36;05:06;11:57; 3:18;06:47;08:47 ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) Array ( [0] => ;;;;;;; ) CSV File has been successfully Imported
我的文件就像这样
答案 0 :(得分:0)
你应该解析你的数据
请显示您的$ emapData
例如,您可以尝试:
$data = explode(",",$emapData);
检查$ emapData内容之前:)
答案 1 :(得分:0)
这应该有效。
$file_handle = fopen($filenamesv, "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";
}
fclose($file_handle);
答案 2 :(得分:0)
要处理Excel文件,我们需要使用外部php库。 Php没有内部课程来做到这一点。您正在使用fgetcsv(),该函数是读取CSV文件。对于excel文件,请参阅此链接