3

I am trying to make 3 table tags system. I have 3 table in mysql:

#Articles#
id
article
content

#Tags#
tag_id
tag (unique)

#tagmap#
id
tag-id
articleid

In my submit php I have:

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

This is not working when I submit new tags to article. Mysql not create new tags in my Tags table.

PS. Sorry for bad english.

2
  • What's the problem? Are you getting an error message? Have you tried echo-ing out the queries that are being created by your code? btw you need to make sure you're not vulnerable to SQL injection (search SO or use your favourite search engine to read more about that). Commented Nov 28, 2011 at 12:39
  • When I added new post I have't got error. Post was added normally, but tags not added. I know about SQL injection, but this is draft version. Commented Nov 28, 2011 at 12:44

1 Answer 1

3

You are missing $ sign for 'x' variable. Try like this for both lines.

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

Also I suggest this way, no need to complicate your SQL queries.

$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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now working great, Thanks. I did not know that I need to assign variable to x.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.