php - 如何在 php 和 sql 中将标签插入 3 表系统

标签 php mysql insert tags

我正在尝试制作 3 表标签系统。我在 mysql 中有 3 个表:

#Articles#<br/> id<br/> article<br/> content

#Tags#<br/> tag_id<br/> tag (unique)

#tagmap#<br/> id<br/> tag-id<br/> articleid

在我提交的 php 中我有:

$tags= explode(',', strtolower($_POST['insert_tags']));

for ($x = 0; $x < count($tags); $x++) {
    //Add new tag if not exist
    $queryt = "INSERT INTO `tags` (`tag_id`, `tag`) VALUES ('', '$tags[x]')";
    $maket = mysql_query($queryt);

    //Add the relational Link, now this is not working, beacasue this is only draft
    $querytm = "INSERT INTO `tagmap` (`id`, `tagid`, `articleid`) VALUES ('',  (SELECT `tag_id` FROM `tags` WHERE tag_id  = "$tags[x]"), '$articleid')";
    $maketm = mysql_query($querytm);
    }

当我向文章提交新标签时,这不起作用。 Mysql 不在我的标签表中创建新标签。

附言。抱歉英语不好。

最佳答案

您缺少“x”变量的 $ 符号。对两条线都这样尝试。

'" . $tags[$x] . "'

我也建议采用这种方式,无需使您的 SQL 查询复杂化。

$tags= explode(',', strtolower($_POST['insert_tags']));
for ($x = 0; $x < count($tags); $x++) {
    //Add new tag if not exist
    $queryt = "INSERT INTO `tags` (`tag_id`, `tag`) VALUES ('', '" . $tags[$x] . "')";
    $maket = mysql_query($queryt);

    //Get tag id
    $tag_id = mysql_insert_id();

    //Add the relational Link, now this is not working, beacasue this is only draft
    $querytm = "INSERT INTO `tagmap` (`id`, `tagid`, `articleid`) VALUES ('',  '$tag_id', '$articleid')";
    $maketm = mysql_query($querytm);
}

关于php - 如何在 php 和 sql 中将标签插入 3 表系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8295779/

相关文章:

php - 不会按 id 过滤更新查询

mysql - 在 Cocoa 应用程序中使用 MySQL

php - 使用 PHP 从数据库获取图像

c++ - 插入到特里

php - Laravel Eloquent 地与Trashed关系

php - 从 NuSOAP 迁移到 PHP5 SOAP

MySQL 数据库设计 - 将这些字段放入用户表中还是创建新表?

php - 在同一页面显示计算结果

php - MYSQL 存储 id 或描述性文本以提高性能

php - 在 Codeigniter 中运行多个分号分隔的查询