Didn't find an answer using the search, so here's my problem

I have a form in which lists of names are submitted and turned into two arrays, lets call them listA and listB. The names from listA get inserted as new rows in MySQL tableA and tableB.

The names from listB should be inserted only in tableB, and only if that name hasn't already been inserted from listA.

Here's my code, but I'm running into issues when I process listB. The first name it finds that was also in listA still gets entered into tableB (causing a duplicate entry) and it fails to insert the last name in listB that does NOT match a name from listA.

  // create records in tableA and tableB from listA
  foreach($listA as $nameA) {
        // insert into tableA
        $query1 = "INSERT INTO tableA (AssurIndex, person_id, LogIndex) VALUES ('', '$nameA', '$LogIndex')";
        mysql_query($query1)  or die ("Error in query1: $query1. " . mysql_error());
        // insert into tableB
        $query2 = "INSERT INTO tableB (PerIndex, person_id, LogIndex) VALUES ('', '$nameA', '$LogIndex')";
        mysql_query($query2)  or die ("Error in query2: $query2. " . mysql_error());
        }
     }

  // create entries in tableB from listB only if not already entered from listA
  foreach($listB as $nameB) {
     // check to be sure this name hasn't been added via listA
     if(array_search($nameB, $listA) == FALSE) {
        // add record to tableB
        $query3 = "INSERT INTO tableB (PerIndex, person_id, LogIndex) VALUES ('', '$nameB', '$LogIndex')";
        mysql_query($query3)  or die ("Error in query3: $query3. " . mysql_error());
        }
     }

    OK, in the second foreach, I was searching on top of the same array...so that is fixed.

    Now the only problem is I am still getting double entries into tableB from the first name that matches between the two lists.

      Ahh.... tried array_diff(); and it was a much simpler solution, and better yet it works perfectly now.

        Write a Reply...