上传csv文件时,它会检查是否已插入电子邮件和电话号码。
我遇到了检查这些输入的if条件的问题。
对于mysqli_num_rows()函数,我没有得到任何值,也会为CSV问题创建任何问题。
<?php
if(isset($_POST["Import"])){
$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$contractorId=$_SESSION['id'];
$flag=false;
$SQLCheckEmail = "SELECT * FROM user where USER_EMAIL='$emapData[1]'";
$result_check_Email = @mysqli_query($conn,$SQLCheckEmail);
$row_check_Email=mysqli_num_rows($result_check_Email);
if($row_check_Email>0){
$flag=true;
echo "This user email ".$emapData[1]." has already been added.";
}
if($flag!=true){
//It wiil insert a row to our subject table from our csv file`
$sql = "INSERT into user (`contractor_id`,`USER_EMAIL`, `USER_ADDRESS`, `USER_PHONE`, `USER_NAME`,USER_SEX,USER_AGE,USER_BIRTHDAY)
values('$contractorId','$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$emapData[7]')";
//we are using mysql_query function. it returns a resource on true else False on error
$result = mysql_query( $sql, $conn );
if(! $result )
{
echo "<script type=\"text/javascript\">
alert(\"????????????CSV?????????????????\");
</script>";
}
}
}
fclose($file);
if($flag!=true){
//throws a message if data successfully imported to mysql database from excel file
echo "<script type=\"text/javascript\">
alert(\"CSV???????????????\");
window.location = \"index.php\"
</script>";
}
//close of connection
mysql_close($conn);
}
}
?>
答案 0 :(得分:0)
我不会在应用程序级别上解决这个问题。让数据库为您完成工作。对于每个记录执行单独的select
语句将导致可怕的性能。
如果根据列email
已存在条目,您似乎要检查。在该列上有一个unique
索引。然后你可以简单地做
LOAD DATA INFILE 'file_name.csv'
IGNORE /*with this it checks based on the unique index on email, if the record already exists*/
INTO TABLE tbl_name /*adjust your table name*/
FIELDS TERMINATED BY ',' /*adjust the following to your CSV file*/
OPTIONALLY ENCLOSED BY '"'
(email_column, another_column) /*name the columns of your table*/
;
您可以阅读有关此命令here的更多信息。