【问题标题】:How to insert tags into 3 table system in php and sql如何在 php 和 sql 中将标签插入到 3 表系统中
【发布时间】:2011-11-28 12:26:09
【问题描述】:

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

#Articles#
id
article
content

#Tags#
tag_id
tag (unique)

#tagmap#
id
tag-id
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 不会在我的 Tags 表中创建新标签。

PS。抱歉英语不好。

【问题讨论】:

  • 有什么问题?您收到错误消息吗?您是否尝试过回显您的代码正在创建的查询?顺便说一句,您需要确保您不会受到 SQL 注入的影响(搜索 SO 或使用您最喜欢的搜索引擎了解更多信息)。
  • 当我添加新帖子时,我没有收到错误。帖子已正常添加,但未添加标签。我知道 SQL 注入,但这是草稿版本。

标签: php mysql insert tags


【解决方案1】:

'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);
}

【讨论】:

  • 感谢回复,我试试这个。
  • 现在工作得很好,谢谢。我不知道我需要将变量分配给 x。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2013-10-07
  • 2010-12-31
  • 2017-06-17
相关资源
最近更新 更多