我正在尝试制作 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/