Reputation: 443
I have two tables, one is 'tags' that stores "tid" (auto increment) and "name" (name of tag). The second table "tags_data" stores the data on each tag. This table has the fields "tid" (from the first table) and a few others.
This is so people can tag content on my website. When I tag content I want to first check if that tag already exists in the first table. If it doesnt exist then we insert the tag into the DB and use the tid to insert into the second table. I have this part working so far.
The problem is when the tag already exists in the DB, I want to grab the existing tid and use it in my second query.
This is the code I have so far:
// check if tag already exists
$tagexists = "SELECT COUNT(*) as cnt FROM tags WHERE 'name' = '$usetag' LIMIT 1";
$results = mysql_query($tagexists) or die('Invalid query: ' . mysql_error());
if ($results['cnt'] == 0) {
// tag is not yet in DB
$tagquery1 = "INSERT INTO tags (name) VALUES ('$usetag')";
$result = mysql_query($tagquery1) or die('Invalid query: ' . mysql_error());
$lastid = mysql_insert_id();
$tagquery2 = "INSERT INTO tags_data (tid, nid, uid) VALUES ($lastid, $nid2, $uid)";
$result = mysql_query($tagquery2) or die('Invalid query: ' . mysql_error());
} else {
// tag is already in DB, grab the tid
$grabtid = "SELECT tid FROM tags WHERE 'name' = '$usetag' LIMIT 1";
$results = mysql_query($grabtid) or die('Invalid query: ' . mysql_error());
$row = mysql_fetch_array($results);
$lastid = $row['tid'];
$tagquery2 = "INSERT INTO tags_data (tid, nid, uid) VALUES ($lastid, $nid2, $uid)";
$result = mysql_query($tagquery2) or die('Invalid query: ' . mysql_error());
}
There is something wrong with the way I am checking if a tag exists already. I was following a guide online using COUNT but it doesnt seem to be working properly.
Upvotes: 0
Views: 1125
Reputation: 143
I think your code is wrong
$tagexists = "SELECT COUNT(*) as cnt FROM tags WHERE 'name' = '$usetag' ";
$res = mysql_query($tagexists) or die('Invalid query: ' . mysql_error());
$results = mysql_fetch_assoc($res);
if ($results['cnt'] == 0) {
// tag is not yet in DB
$tagquery1 = "INSERT INTO tags (name) VALUES ('$usetag')";
$result = mysql_query($tagquery1) or die('Invalid query: ' . mysql_error());
$lastid = mysql_insert_id();
$tagquery2 = "INSERT INTO tags_data (tid, nid, uid) VALUES ($lastid, $nid2, $uid)";
$result = mysql_query($tagquery2) or die('Invalid query: ' . mysql_error());
} else {
// tag is already in DB, grab the tid
$grabtid = "SELECT tid FROM tags WHERE 'name' = '$usetag' LIMIT 1";
$results = mysql_query($grabtid) or die('Invalid query: ' . mysql_error());
$row = mysql_fetch_array($results);
$lastid = $row['tid'];
$tagquery2 = "INSERT INTO tags_data (tid, nid, uid) VALUES ($lastid, $nid2, $uid)";
$result = mysql_query($tagquery2) or die('Invalid query: ' . mysql_error());
}
Upvotes: 1
Reputation: 6758
$results['cnt'] == 0
is wrong. You are forgetting to get the result. ($results is a resource id.)
if(mysql_result($results) == 0){
Likewise, your second part of the if
that gets the existing data is missing it too. You want to use $data = mysql_fetch_assoc($result);
there.
Upvotes: 1