微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在PHP和SQL中将标签插入3表系统

我正在尝试使3表标签系统.我在mysql中有3个表:

#文章#
ID
文章
内容

#标签#
tag_id
标签(唯一)

#tagmap#
ID
标签编号
商品编号

在我的提交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不在我的标签表中创建新标签.

PS.对不起,英语不好.

解决方法:

您缺少’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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐