如何在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);
}
上一篇:没有数据库的PHP Tag系统(纯文本文件)


下一篇:Elastic Search的聚合搜索