SchweppesAle Posted March 11, 2009 Share Posted March 11, 2009 hi, I'm trying to place the following array into a table, however I'm having some trouble avoiding duplicate entires. You'll see from the code that I initially tried running the array and table rows through a loop statement. Unfortunately, something was wrong with my statement so all variables(including duplicates) where entered into the table. So I did some looking around and found the following suggestion for a similiar problem. It looked something like this: mysql_query("INSERT INTO jos_AuthorList (AuthorID, AuthorName) VALUES('$ID', '$Name') WHERE ('$ID') NOT IN(SELECT AuthorID FROM jos_AuthorList") or die(mysql_error()); needless to say I was pretty psyched until it returned the following error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ('62') NOT IN(SELECT * FROM jos_AuthorList' at line 2 Here's actual code so far. You'll have to excuse any garbage variables, I've been experimenting with this for a while. global $mainframe; $db =&JFactory::getDBO(); $query = "SELECT * FROM #__AuthorList"; $db->setQuery( $query, 0, $count ); $rows = $db->loadObjectList(); echo "no duplicates"; $rowcounter = 0; /*counts number of rows in AuthorList table*/ foreach($rows as $row) { $rowcounter++; } for ($x = 0; $x < count($AuthorID); $x++) { $duplicates = 0; foreach($rows as $row) { if ($AuthorID[$x][0] == $row->AuthorID) { $duplicates++; } } /* if($duplicates == 0) {*/ /*allows us to insert data into jos_AuthorList table*/ $ID = $AuthorID[$x][0]; $Name = $AuthorID[$x][1]; mysql_query("INSERT INTO jos_AuthorList (AuthorID, AuthorName) VALUES('$ID', '$Name') WHERE ('$ID') NOT IN(SELECT AuthorID FROM jos_AuthorList") or die(mysql_error()); echo "<br/>"; echo "inserted"; echo "<br/>"; echo $AuthorID[$x][0]; echo "<br/>"; echo $AuthorID[$x][1]; /*}*/ } Thanks again for your help. Quote Link to comment Share on other sites More sharing options...
SchweppesAle Posted March 11, 2009 Author Share Posted March 11, 2009 oh man, INSERT IGNORE is the best thing ever. lol, I figured it out. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.