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

  • SEARCH
  • Home
  • 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 9151623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:50:53+00:00 2026-06-17T11:50:53+00:00

I’m having issues with the current code, actually I’m not sure if I’m going

  • 0

I’m having issues with the current code, actually I’m not sure if I’m going about the right way either way.

My current script (the total) uploads a photo and returns the id from the MySQL database. When the photo is selected, it’s also been given tags. To associate these tags, I have chosen to have a table ‘tagrefs’ which simple combine tag_id with photo_id for later querying. The table ‘tags’ consists of tag_id and tag_name. The table ‘photos’ has tons of fields, the only relevant one being photo_id.

Currently, I pass the tags via POST.

      $tags = trim($_POST['tags']);
  $tags = preg_replace('/\s+/', '', $tags);
  $tags = htmlspecialchars($tags);
  $tags = mysql_real_escape_string($tags);
  $tags = strip_tags($tags);
  $tags = explode(",", $tags);

$insert = $dbh->prepare('INSERT INTO tags (tag_name) VALUES (:tag)');
$insert->bindParam(':tag', $tag, PDO::PARAM_STR);
foreach ($tags as $tag) $insert->execute();


$select = $dbh->prepare('SELECT tag_id FROM tags WHERE tag_name = (:tag)');
$select->execute();
$result = $select->fetch(PDO::FETCH_ASSOC);
print_r($result);
print("\n");

Up until $select it works perfectly. Tags are inserted, photo is uploaded. Peachy. Now I need to write something that will give me the tag_id of the tag_name s I just added, then I need to combine that with the photo_id and insert that into ‘tagrefs’. The bottom $select is my failing attempt to retrieve said tag_id’s that I just created by comparing them to the string the user POSTs. The manner I was thinking of is retrieving them, then creating a new array with the photo_id and the tag_id s and inserting the values into ‘tagrefs’.

It all sounds overly complicated and there is probably a much better/safer way to do it but an hour of Googling has not helped.

  • 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-17T11:50:54+00:00Added an answer on June 17, 2026 at 11:50 am
    foreach ($tags as $tag) $insert->execute();
    

    is incorrect. There’s no {}, so all the foreach is doing is that one execute call, over and over. The rest of the code afterwards is NOT executed as part of the foreach. As such, you insert all of the tags, then when the loop is completed, you try to fetch the id of the LAST tag you inserted. Assuming this tag_id is an auto_increment primary key field, you should be using something like this:

    foreach($tags as $tag) {
        $insert->execute();
        $tag_id = $insert->lastInsertId();
        ....
    }
    

    and not doing a dedicated select anyways.

    • 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
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm interested in microtypography issues on the web. I want a tool to fix:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an autohotkey script which looks up a word in a bilingual dictionary

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.