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 8563659
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:00:38+00:00 2026-06-11T17:00:38+00:00

I’m having some problems with the code below. What I’m trying to do is

  • 0

I’m having some problems with the code below. What I’m trying to do is to tell if a tag already exists in the table tags, if it does, get the id and put that id in the table blog_post_tags, or if it does not exist, create it, get the id and put it in blog_post_tags table. The problem is that, whenever I try to check if the id already exists, I get errors all over the place telling me trying to get property of non object but fields are still being insert in the other table with an id of 0 (which is not a valid id for the tags).

Here is the code:

foreach ($tags as $tag)
{
    //search for the tag to see if it exists 
    //the problem is located in this line of code
    if($select_stmt = $mysqli->prepare("SELECT id FROM tags WHERE name = ?"))
    {
        $select_stmt->bind_param("s",$tag);
        $select_stmt->execute();
        $result = $select_stmt->get_result();

        //if the tag exists
        if($select_stmt->num_rows != 0)
        {
            if($insert_stmt = $mysqli->prepare("INSERT INTO blog_post_tags (blog_post_id, tag_id) VALUES (?,?)"))
            {
                $insert_stmt->bind_param('ii', $blogid, $result);
                $insert_stmt->execute();
            }
        }
        else
        {
            if ($insert_stmt2 = $mysqli->prepare("INSERT INTO tags (name) VALUES (?)")) 
            {
                $insert_stmt2->bind_param('s', $tag);
                $insert_stmt2->execute();
                $tagid = $mysqli->insert_id;

                if ($insert_stmt3 = $mysqli->prepare("INSERT INTO blog_post_tags (blog_post_id, tag_id) VALUES (?, ?)"))
                {
                    $insert_stmt3->bind_param('ii', $blogid, $tagid);
                    $insert_stmt3->execute();
                }
            }
        }
    }
}

Thanks in advance for the help.

EDIT

Some how, the “trying to get property of a non object” disappeared, i suspect it may have been a connection erro with the server but i really cant tell, still the table blog_post_tags is being filled with fields that does not make any sense, “tag_id =0”. i suspect it may have something to do with the problem expressed here: php prepared stmt problem- update data at each result loop
¿ how could i correct me code if the problem is related to that of the link above ?

  • 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-06-11T17:00:40+00:00Added an answer on June 11, 2026 at 5:00 pm

    Well, i really dont know what it did, but it seem to have worked, here is the code without any errors:

    foreach ($tags as $tag)
            {
                //search for the tag to see if it exists
                if ($select_stmt = $mysqli->prepare("SELECT id FROM tags WHERE name = ?")) 
                {
    
                    $select_stmt->bind_param("s", $tag);
                    $select_stmt->execute();
                    $select_stmt->store_result();
    
                    if ($select_stmt->num_rows() > 0) 
                    {
    
                        $select_stmt->bind_result($tagid);
                        $select_stmt->fetch();
                        if ($insert_stmt = $mysqli->prepare("INSERT INTO blog_post_tags (blog_post_id, tag_id) VALUES (?, ?)"))
                        {
                            $insert_stmt->bind_param('ii', $blogid, $tagid);
                            $insert_stmt->execute();
                        }
                    }
                    else
                    {
                        if ($insert_stmt2 = $mysqli->prepare("INSERT INTO tags (name) VALUES (?)"))
                        {
                            $insert_stmt2->bind_param('s', $tag);
                            $insert_stmt2->execute();
                            $tagid = $mysqli->insert_id;
    
                            if ($insert_stmt3 = $mysqli->prepare("INSERT INTO blog_post_tags (blog_post_id, tag_id) VALUES (?, ?)"))
                            {
                                $insert_stmt3->bind_param('ii', $blogid, $tagid);
                                $insert_stmt3->execute();
                            }
                        }
                    }
                }
             }
    

    hopefully this can help anyone that encounters a similar problem, also, i would like to know if this solution is optimal or if there is any way to improve it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters

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.