mysql - Check duplicate before inserting in database from CSV upload in PHP -
when uploading csv file check whether email , phone numbers inserted or not.
i facing problems if condition checks these inputs.
for mysqli_num_rows() function, not getting values , create problem csv problem.
<?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 * user 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 been added."; } if($flag!=true){ //it wiil insert row our subject table our csv file` $sql = "insert 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 using mysql_query function. returns 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 message if data imported mysql database excel file echo "<script type=\"text/javascript\"> alert(\"csv???????????????\"); window.location = \"index.php\" </script>"; } //close of connection mysql_close($conn); } } ?>
i wouldn't solve on application level. let database job you. each record doing separate select
statement result in horrible performance.
you seem check, if entry exists based on column email
. have unique
index on column. can do
load data infile 'file_name.csv' ignore /*with checks based on unique index on email, if record exists*/ table tbl_name /*adjust table name*/ fields terminated ',' /*adjust following csv file*/ optionally enclosed '"' (email_column, another_column) /*name columns of table*/ ;
you can read more command here.
Comments
Post a Comment