TID  CID  Name
 1    1   Wordpress
 2    1   Facebook
 3    1   Facebook - This should not come!
 4    2   Facebook - This is allowed
So, CID and Name, combined together, uniquely identifies the record. Got it? How to achieve this?
I thought of restricting the duplicate entries by this php script:

PHP Code:
if(mysql_num_rows("SELECT * FROM `tags` WHERE `cid` = ".$lcid." AND `name` = '".$ltag."';") == 0)
mysql_query("INSERT INTO `tags` ( `tid` , `cid` , `name` ) VALUES ( NULL , '".$lcid."', '".trim($ltag)."')"))
'<br>Successfully added '.$ltag.' to '.$_POST['flname'].'.';
'<br>Cannot add '.$ltag.' to '.$_POST['flname'].'. Reason: '.mysql_error();
'<br>Cannot add '.$ltag.' to '.$_POST['flname'].'. Reason: Tag already exists!'
But its not working! Please help me.