Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 706513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:10:30+00:00 2026-05-14T04:10:30+00:00

I am stuck on how to create tags for each post on my site.

  • 0

I am stuck on how to create tags for each post on my site. I am not sure how to add the tags into database.
Currently…
I have 3 tables:

+---------------------+    +--------------------+    +---------------------+
| Tags                |    | Posting            |    | PostingTags         |
+---------------------+    +--------------------+    +---------------------+
| + TagID             |    | + posting_id       |    | + posting_id        |
+---------------------+    +--------------------+    +---------------------+
| + TagName           |    | + title            |    | + tagid             |
+---------------------+    +--------------------+    +---------------------+

The Tags table is just the name of the tags(ex: 1 PHP, 2 MySQL,3 HTML)
The posting (ex: 1 What is PHP?, 2 What is CSS?, 3 What is HTML?)
The postingtags shows the relation between posting and tags.

When users type a posting, I insert the data into the “posting” table. It automatically inserts the posting_id for each post(posting_id is a primary key).

    $title = mysqli_real_escape_string($dbc, trim($_POST['title']));
    $query4 = "INSERT INTO posting (title) VALUES ('$title')";
    mysqli_query($dbc, $query4);

HOWEVER, how do I insert the tags for each post?
When users are filling out the form, there is a checkbox area for all the tags available and they check off whatever tags they want. (I am not doing where users type in the tags they want just yet)

This shows each tag with a checkbox. When users check off each tag, it gets stored in an array called “postingtag[]”.

<label class="styled">Select Tags:</label>
    <?php

 $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
 $query5 = "SELECT * FROM tags  ORDER BY tagname";
 $data5 = mysqli_query($dbc, $query5);
 while ($row5 = mysqli_fetch_array($data5)) {
    echo '<li><input type="checkbox" name="postingtag[]"
        value="'.$row5['tagname'].'" ">'.$row5['tagname'].'</li>';
    }

 ?>

My question is how do I insert the tags in the array (“postingtag”) into my “postingtags” table?
Should I…

    $postingtag = $_POST["postingtag"];
foreach($postingtag as $value){
$query5 = "INSERT INTO postingtags (posting_id, tagID) 
               VALUES (____, $value)";
mysqli_query($dbc, $query5);
}       

1.In this query, how do I get the posting_id value of the post?

I am stuck on the logic here, so if someone can help me explain the next step, I would appreciate it!

Is there an easier way to insert tags?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:10:30+00:00Added an answer on May 14, 2026 at 4:10 am

    PostingTags is a Many-To-Many mapping table. You are correct in your assessment of the data you need, but I don’t see how we can help you find it.

    1.In this query, how do I get the posting_id value of the post?
    Does your application not know this when the user is selecting tags? You’re going to need to know what post is actually being edited before you can assign tags to it. Is this a completely separate page where the user is picking tags? If it is, you’ll need to create a hidden field in your webform that passes the posting_id from one page to the next.

    Otherwise, it just becomes an issue of determining the last primary key used when you inserted into the postings table. For that, you need to call mysqli::insert_id like this:

    //This is the first snippet of code you posted
    $title = mysqli_real_escape_string($dbc, trim($_POST['title']));
    $query4 = "INSERT INTO posting (title) VALUES ('$title')";
    mysqli_query($dbc, $query4);
    $posting_id = $dbc->insert_id;
    

    I am stuck on the logic here, so if someone can help me explain the next step, I would appreciate it!
    Do you understand now?

    Is there an easier way to insert tags?
    Not if you want your users to be able to insert an arbitrary number of tags to a post.

    My question is how do I insert the tags in the array (“postingtag”) into my “postingtags” table?
    Your code does the job fine, though I would be doing everything as prepared statements. Prepared statements prevent SQL injection attacks so that you don’t have to remember to escape everything that goes into the query. It’s also a much less verbose way of doing things:

    //This is the last snippet of code you posted
    //Populate postid as specified in the first part of this answer.
    $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    foreach($postingtag as $tag) {
        $sql = 'INSERT INTO postingtags (posting_id, tagID) VALUES (?, ?);';
        $insertStatement = $dbc->prepare($sql);
        $insertStatement->bind_param('ii', $postid, $tag);
        $insertStatement->execute();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following tables; CREATE TABLE IF NOT EXISTS `tags` ( `tag_id` int(11)
I have 2 tables: CREATE TABLE article ( id serial NOT NULL, title text,
...or am I stuck rolling my own XML chopping functions. I'd like to create
Being stuck with a legacy database schema that no longer reflects your data model
I'm trying to create related articles for our news, based on tags (single word
I am going through the android hello world tutorial, and have been stuck when
I have a page containing a series of tags like this <a class=IMG href=#
I've been stuck for a while now on how to create a Coming Soon
I am trying to create 10.000 hexagon connected each other like bee combs.I want
I am trying to create a site-specific browser application. I've created a new Cocoa

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.