Eğer aşağıdaki gibi bir tablo yapısı olduğunu söylüyorlar:
id tag count
Hangi birincil anahtar olarak tag olmadıkça geçerli sorgu çalışmaz anlamına gelir. Bu durum buysa, muhtemelen birincil anahtar olarak id sütunu kullanarak ediyoruz.
Bu yüzden sakin ol ve sadece etiket zaten orada olup olmadığını görmek için kontrol edin.
Eğer öyleyse, güncelleyecektir. Değilse, bunu ekleyin.
//--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);