Question

I am importing an excel file to DB. For that i am using excel-class reader. Using that i can insert values to DB directly. Here is my code to insert data.

$q="SELECT * FROM leads_info WHERE  name='".$rows[1]."' AND home_phone='".$rows[2]."' AND mobile_phone='".$rows[3]."'  AND address='".$rows[4]."' AND suburb='".$rows[5]."' AND postcode='".$rows[6]."'";
$r= mysql_query($q) or die(mysql_error());
$row = mysql_fetch_array($r);
if(empty($row))
     {
$sql = "INSERT INTO leads_info VALUES('','$rows[1]','$rows[2]','$rows[3]','$rows[4]','$rows[5]','$rows[6]')";
$result=mysql_query($sql) or die(mysql_error());
     } 

Actually this insertion is in a loop. That loop is inserting each row of an excel file.Before inserting the data, selecting all the duplicates from DB and stored it in an array(ie:$row). If $row is empty($row doesn't have the excel row), inserting the data.But it didn't work.

Was it helpful?

Solution

Instead of:

$row = mysql_fetch_array($r);
if(empty($row))

do:

if(!mysql_num_rows($r))

BTW, you have some SQL injection issues there. (Google that to learn more about it.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top