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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.