You say that the structure of your table is as follows:
id tag count
Which means your current query won't work unless you have tag
as the primary key. If that's the case, you're probably using the id
column as the primary key.
So take it easy and just check to see if the tag is already there.
If so, update the count. If not, add it.
//--grab the tag
$tag = mysql_real_escape_string($_POST['tag']);
//--see if the tag already exists and potentially what the current count is
$query = "SELECT id, count FROM tags WHERE tag='$tag'";
$result = mysql_query($query);
//--if there is a row, that means the tag exists
if(mysql_num_rows($result))
{
//--pull out the tag ID and the current count and increment by one.
$tag_info = mysql_fetch_array($result);
$tag_info_id = $tag_info["id"];
$tag_info_count = $tag_info["count"] + 1;
//--update the table with the new count
$sql_update_cnt = "UPDATE tags SET count='$tag_info_count'
WHERE id='$tag_info_id'";
mysql_query($sql_update_cnt);
echo "$tag now with $tag_info_count instances";
}
else
{
//--tag is not there, so insert a new instance and 1 as the first count
$query = "INSERT INTO tags (tag, count) VALUES ('$tag', 1)";
mysql_query($query);
echo "1 record added";
}
mysql_close($dbc);