Hi,

I am trying to check MYSQL table field for existing Tags. If they exist i dont want it to insert the tag.

If they dont exist i want it to run the INSERT query.

My code below wont work though.. it just keeps returning already exists even when the tag dosent exist in the table.

Can anyone plese tell me what i am doing wrong and maybe help with an example.

Thanks in advance :-)

//make variables safe 
  $tagname = mysql_real_escape_string($_POST['tag']);
  $status = 0;
  $new = 1;
  
	//get existing tagnames first
	
	$sql1 = "SELECT tag_name FROM Tags";
	$result1 = mysql_query($sql1);
	while($tagrow = mysql_fetch_array($result1))
	{ 
	$existingTags = $tagrow['tag_name'];     	
	}
	
	if( $existingTags = $tagname ) {
	
	echo "tag allready exists!";
	
	} else {

	//insert new tag 
	
	$sql = "
		INSERT INTO Tags
		SET
		tag_name = '$tagname'";
	$result = mysql_query($sql);
	
	}

Try the code below
Example 1;

<?php

$tagname = mysql_real_escape_string($_POST['tag']);
$sqll = "SELECT COUNT(tagname) FROM mm WHERE tagname = '$tagname'";
$result = mysql_query($sqll) or die(mysql_error());
$status = mysql_fetch_row($result);

if($status[0] > 0)
{
	//Tag exist
	echo "Exist";
}
else
{
	//Tag does not exist, do whatever you want here
	echo "Does not exist";
}

?>

Exammple2:

<?php
$status = 0;
$tagname = mysql_real_escape_string($_POST['tag']);
$sqll = "SELECT tag_name FROM tags";
$result = mysql_query($sqll) or die(mysql_error());
while($tagrow = mysql_fetch_array($result))
{
	if($tagrow[tag_name] == $tagname)
	{
		$status++;
	}
}

if($status > 0)
{
	//Tag exist notify user

}
else
{
	//Tag does not exist, do whatever you want here

}



?>

Hello cossay,

thank you very much :-)

I'll try this today and let you know.

Thanks so much again.

John

If it works for you don't forget to mark it solved.

Hi Cossay,

I should have left the variables for status and new out of the code. They only insert single digits so i can process the entries later.

What i really need to do is check if the POSTED value

['tag']

is already in the field name

tag_name

Sorry to confuse the issue :-)

John

Actually scrap that!

Your first example worked a treat!

Thank you so much, i spent all of this morning trying to work that out.

And best of all i understand your method too.

Cheers Cossay :-)

The POSTED value

$_POST['tag']

is stored in

tag_name

as a result of the statement

$tagname = mysql_real_escape_string($_POST['tag']);

The POSTED value

$_POST['tag']

is stored in

tag_name

as a result of the statement

$tagname = mysql_real_escape_string($_POST['tag']);

Actually scrap that!

It does work, i just mis understood the use of the variable $status because i was using that variable to insert a zero.

But i see that you used it to do the check count :-)

Your first example worked a treat!

Thank you so much, i spent all of this morning trying to work that out.

And best of all i understand your method too.

Cheers Cossay :-)

In case you are wondering, the problem in your ORIGINAL code is in line 16. You check for equality with TWO consecutive equal signs: if( $existingTags [B]==[/B] $tagname )

In case you are wondering, the problem in your ORIGINAL code is in line 16. You check for equality with TWO consecutive equal signs: if( $existingTags [B]==[/B] $tagname )

Thank you hielo,

i do see that now.

John

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.