Jump to content

[SOLVED] Inserting into MYSQL while avoiding duplicate entries


SchweppesAle

Recommended Posts

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.