我遇到了导入MySQL数据库的CSV问题:
<div style="border:1px dashed #333333; width:300px; margin:0 auto; padding:10px;">
<form name="import" method="post" enctype="multipart/form-data">
<input type="file" name="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
include ("connection.php");
if(isset($_POST["submit"]))
{
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
**while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{
$name = $filesop[0];
$email = $filesop[1];
$sql = mysql_query("INSERT INTO selleruser (emailid, pass) VALUES ('$name','$email')");
$c = $c + 1;
}**
if($sql){
echo "You database has imported successfully. You have inserted ". $c ." recoreds";
}else{
echo "Sorry! There is some problem.";
}
}?>
</div>
我收到了这个错误:
未定义的偏移量:此处为1行$ email = $ filesop 1;
答案 0 :(得分:0)
看起来您遇到了分隔符问题。
fgetcsv($ handle,1000,“,”)
如果我使用分隔符“;”保存文件,我可以重现此问题。 因此,您可能会发现保存了分隔符文件并使用它:
<?php
function detectDelimiter($csvFile)
{
$delimiters = array(
';' => 0,
',' => 0,
"\t" => 0,
"|" => 0
);
$handle = fopen($csvFile, "r");
$firstLine = fgets($handle);
fclose($handle);
foreach ($delimiters as $delimiter => &$count) {
$count = count(str_getcsv($firstLine, $delimiter));
}
return array_search(max($delimiters), $delimiters);
}
#print_r(detectDelimiter('chart_column.csv'));
$handle = fopen('chart_column.csv', "r");
$delimiter = detectDelimiter('chart_column.csv');
echo "I checked file and found that its delimiter is \"$delimiter\". Will use it further. \n";
$c = 0;
while(($filesop = fgetcsv($handle, 1000, $delimiter)) !== false)
{
$name = $filesop[0];
$email = $filesop[1];
echo "emailid = $name , pass = $email\n";
# $sql = mysql_query("INSERT INTO selleruser (emailid, pass) VALUES ('$name','$email')");
$c = $c + 1;
}
结果我得到了:
~$ cat chart_column.csv
emailid;pass
asd;asd
fdga;ewrdf
asdf;56gfh
fgh5;fgh
dfg;dfgdf
dfg;dfgdf
~$ php delimiter.php
I checked file and found that its delimiter is ";". Will use it further.
emailid = emailid , pass = pass
emailid = asd , pass = asd
emailid = fdga , pass = ewrdf
emailid = asdf , pass = 56gfh
emailid = fgh5 , pass = fgh
emailid = dfg , pass = dfgdf
emailid = dfg , pass = dfgdf
希望,这会有所帮助。