0

I have two tables tmp_table, test_contacts. tmp_table is created from a csv upload. Sometimes in the csv upload we will have an email already in the test_contacts table. Here is the code I have to compare update/insert. The IF statement is being executed fine but mysql_query inside the ELSE is not working.

$query = "SELECT id FROM test_contacts ORDER BY id DESC LIMIT 1";
$result = mysql_query($query, $conn);
while($rows=mysql_fetch_assoc($result)){
      echo $rows['id'];


 $duplicate = mysql_query("SELECT test_contacts.email, tmp_table.email FROM test_contacts, tmp_table WHERE test_contacts.email = tmp_table.email");
if (mysql_num_rows($duplicate) > 0) {
   mysql_query("UPDATE test_contacts, tmp_table SET test_contacts.accountnum = tmp_table.accountnum, test_contacts.type=tmp_table.type, test_contacts.rep=tmp_table.rep, test_contacts.lname=tmp_table.lname, test_contacts.fname=tmp_table.fname WHERE test_contacts.email=tmp_table.email");
} else {
   mysql_query("INSERT INTO test_contacts (account,source,accountnum,tags,type,rep,lname,fname,title,company,address,addresstwo,city,state,zip,email,cell,officenum,email_status,date,country) SELECT tmp_table.account,tmp_table.source,tmp_table.accountnum,tmp_table.tags,tmp_table.type,tmp_table.rep,tmp_table.lname,tmp_table.fname,tmp_table.title,tmp_table.company,tmp_table.address,tmp_table.addresstwo,tmp_table.city,tmp_table.state,tmp_table.zip,tmp_table.email,tmp_table.cell,tmp_table.officenum,tmp_table.email_status,tmp_table.date,tmp_table.country FROM tmp_table");
} 

  echo "<h2>Contacts Uploaded</h2>";
mysql_query ('TRUNCATE TABLE tmp_table;');
  mysql_close($conn);

2 Answers 2

1

What you want to do is probably better accomplished with ON DUPLICATE KEY UPDATE). For this to work, you need to identify the column or columns that specify what a duplicate is. In this case, I think it is email. So you need to declare email to be the primary key on the table or to have a unique index.

Then the insert code looks something like this:

INSERT INTO test_contacts(account, source, accountnum, tags, type, rep, lname, fname, title,
                          company, address, addresstwo, city, state, zip, email, cell, officenum, 
                          email_status, date, country
                         )
    SELECT tt.account, tt.source, tt.accountnum, tt.tags, tt.type, tt.rep, tt.lname,
           tt.fname, tt.title, tt.company,tmp_table.address, tt.addresstwo, tt.city, tt.state,
           tt.zip, tt.email, tt.cell, tt.officenum, tt.email_status, tt.date, tt.country
    FROM tmp_table tt
    ON DUPLICATE KEY UPDATE account = VALUES(account),
                            . . .
                            country = VALUES(country);

You can fill in the . . . with the rest of the columns to assign.

3
  • I cannot do it this way. There will sometimes be a list where there is no email.In the event of no email the row gets inserted in to test_contacts. After it is inserted in to test_contacts the email is set to = id. I can't figure out why my above code is not working properly.
    – Billy
    Oct 26, 2014 at 1:06
  • @Billy . . . I don't see why that is a problem. Create a unique index on email. Then if it doesn't exist, it will be ok. Unique keys allow duplicates of NULL values. Oct 26, 2014 at 1:26
  • I created a unique index on email and inserted your code in replace of my IF ELSE statement. INSERT and UPDATE do not work now. my PRIMARY key is id (auto increment) and unique key is email. The snippet does not give any errors, but it also does not do anything to the tables.
    – Billy
    Oct 26, 2014 at 1:42
0

Try Insert Query without "INTO"

INSERT test_contacts SELECT * FROM tmp_table;

this is something like copying the rows of one table to another.Hope it will work.

1
  • add a where clause if any check is required...Also this will work only if the able schema is identical Oct 26, 2014 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.