【问题标题】:Mysql, Check if row exists, if it does grab value, if not insertMysql,检查行是否存在,如果它确实获取值,如果不插入
【发布时间】:2012-10-31 17:32:42
【问题描述】:

我有两个表,一个是存储“tid”(自动增量)和“name”(标签名称)的“标签”。第二个表“tags_data”存储每个标签上的数据。此表包含字段“tid”(来自第一个表)和其他一些字段。

这样人们就可以在我的网站上标记内容。当我标记内容时,我想首先检查该标记是否已存在于第一个表中。如果它不存在,那么我们将标签插入数据库并使用 tid 插入第二个表。到目前为止,我已经完成了这部分工作。

问题是当标签已经存在于数据库中时,我想抓取现有的 tid 并在我的第二个查询中使用它。

这是我目前的代码:

    // 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());        
}

我检查标签是否已经存在的方式有问题。我正在使用 COUNT 在线遵循指南,但它似乎无法正常工作。

【问题讨论】:

标签: php mysql insert


【解决方案1】:

$results['cnt'] == 0 是错误的。你忘记了得到结果。 ($results 是一个资源 ID。)

if(mysql_result($results) == 0){

同样,获取现有数据的if 的第二部分也丢失了它。你想在那里使用$data = mysql_fetch_assoc($result);

【讨论】:

  • 我的代码似乎还有其他问题。即使标签已经存在,它似乎也不在乎,它只是再次插入它。还有什么让你印象深刻的吗?编辑:似乎“名称”周围的单引号将计数设为 0。删除它们可以修复它,谢谢
  • 在第二部分的第一个查询中,name 周围又出现了单引号。是否还有其他错误抛出?
【解决方案2】:

我认为你的代码是错误的

$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());        
}

【讨论】:

  • 推荐使用mysql_fetch_assoc(及其变种)
猜你喜欢
  • 2016-01-06
  • 2012-05-21
  • 2013-03-31
  • 1970-01-01
  • 2012-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多