views:

99

answers:

5

I want this script to check to see if the same word has been entered if so it should add it to the count not add a duplicate tag.

And if the tag entered is a new tag then let it add it to the database.

Can some one help me fix this?

$tag = mysql_real_escape_string($_POST['tag']);
$query = "INSERT INTO tags (tag, count) VALUES ('$tag', 1)
          ON DUPLICATE KEY UPDATE count = count+1";
if (!mysql_query($query, $dbc))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($dbc)

THANKS EVERYONE I GOT IT TO WORK BUT ANOTHER PROBLEM CAME UP BUT IT'S A DIFFERENT QUESTION

+2  A: 

Sure, it is easy. Add unique index on 'tag' column in database or make it primary key.

vava
how do I do this?
You would do this in whatever tool you are using to manage your database, for example the MySQL command tool http://dev.mysql.com/doc/refman/5.4/en/mysql.html or phpMyAdmin http://www.phpmyadmin.net/home_page/index.php
Amal Sirisena
A: 

your problem is the id will probably be the primary key, not tag. as i mentioned above your code would only work if tag was the primary key on the table

seengee
+3  A: 

try this query to add a primary key to your tags table

ALTER TABLE tags  ADD PRIMARY KEY (tag);

UPDATE

Just run it in phpmyadmin OR

do this

$sql = "ALTER TABLE tags  ADD PRIMARY KEY (tag)";
$result = mysql_query($sql);

in a test.php

UPDATE 2

OK this means you already have a primary key, you can add a unique index to tag now using this query ..

ALTER TABLE tags ADD UNIQUE (tag);

Please note that if you already have duplicates, then you'll need to remove them first.

Wbdvlpr
where do i add it?
check update to my answer.
Wbdvlpr
it tells me Multiple primary key defined?
Check update 2 ..
Wbdvlpr
A: 

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);
random
I'm trying your solution this is how my database is set up:<pre>CREATE TABLE tags (id int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY(id),tag varchar(100) NOT NULL,count int(11) NOT NULL DEFAULT '0');</pre>for some reason now my databse wont update now:(
Fixed up the error where the query to create a new tag was left out. Try now.
random
A: 

if you’re using mySQL, you can use the INSERT ON DUPLICATE KEY syntax, provided your tagname is the primary key (or unique)

INSERT INTO `tags` (`tag`, `count`)
VALUES ($tag, 1)
    ON DUPLICATE KEY UPDATE `count`=`count`+1;

to add a primary key on your table use:

ALTER TABLE `tags` ADD PRIMARY KEY (`tag`)

you don’t need an id column though, because you can use your tagname as primary key

ALTER TABLE `tags` DROP COLUMN `id`
knittl