注意在导入时保持数据库的编码与txt文档编码尽量一样,要不可能出现乱码问题哦。
注意在导入时保持fopen($_FILES["insert_first"]["tmp_name"],"r") or exit("打开文件失败!");
var_dump($file);
echo "
";
while(!feof($file)){
//逐行分拆
$row = explode(",",fgets($file));
$row0 = $row[0];
$row1 = (string)$row[1];
echo "$row0"."---"."$row1"."
";
//判断记录是否存在
$query1 = "select firstid from firstcategory where firstid=$row0";
$query2 = "select firstname from firstcategory where firstname='$row1'";
$result1 = $con->query($query1);
$result2 = $con->query($query2);
//判断行数
$result1 = mysqli_num_rows($result1);
$result2 = mysqli_num_rows($result2);
if($result1){
echo "行号{$row0}已存在
";
}elseif($result2){
echo "分类{$row1}已存在
";
}else{
$query = "insert into firstcategory (firstid,firstname) values ($row0,'音乐')";
$result = $con->query($query);
}
}
echo "导入结束,请检查数据是否正确 http://www.jquerycn.cn ";
fclose($file);
$con->close();
?>
mysql数据库
"create table firstcategory (
firstid int unsigned not null primary key,
firstname char(50) not null
)";