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

The Archive Base Latest Questions

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

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

  • 0
$insert = $dbh->prepare('INSERT INTO tags (tag_name) VALUES (:tag)');
  $insert->bindParam(':tag', $tag, PDO::PARAM_STR);
  foreach($tags as $tag) {
   $insert->execute();
   $tag_id = $dbh->lastInsertID();
echo $tag_id."+".$photo_id."<br />";
$sql = "INSERT INTO tagrefs (tag_id, photo_id) VALUES (:tag_id,:photo_id)";
$q = $dbh->prepare($sql);
$q->execute(array(':tag_id'=>$tag_id,
                       ':photo_id'=>$photo_id));
  }

This particular piece of code inserts tags related to uploaded photos into a table called ‘tags’. It links the tag_id to the photo_id in a table called ‘tagrefs’. This all works fine, until I use a tag twice. Which is logical, because nothing is inserted (tags are unique, I simply want the entry in ‘tagrefs’ to list the photo_id for my next photo with tag_id’s that already exist)

How do I make it so that my code compares the tags the user put in and compares them, or that the values of existing tags are returned and put into ‘tagrefs’ properly? Thank you very much in advance for your time.

  • 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:24:03+00:00Added an answer on June 17, 2026 at 11:24 am

    If you use INSERT ... ON DUPLICATE KEY UPDATE, then lastInsertID() will return the AUTO_INCREMENT field’s value of a matched row even if an UPDATE is performed instead of an insertion.

    To ensure that it also works in versions of MySQL prior to v5.1.12, one can explicitly set the insertion id with MySQL’s LAST_INSERT_ID() function:

    INSERT INTO tags
      (tag_name)
    VALUES
      (:tag)
    ON DUPLICATE KEY UPDATE
      id = LAST_INSERT_ID(id)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

INSERT INTO [Temp].[dbo].[Student] ([Fname], [Lname], [Gender]) VALUES (N'Aname', N'Alname', N'Male') GO This codes workes
INSERT INTO `ree`.`media` (`CREATEDATE`, `FILETYPE`, `MIMETYPE`, `MLSNUMBER`, `MODIFYDATE`, `POSITION`, `URL`) VALUES ('2011-12-27T15:00:16', 'PRIMARY PHOTO',
INSERT INTO files (fileUID, filename) WITH fileUIDS(fileUID) AS ( VALUES(1) UNION ALL SELECT fileUID+1
insert into Attributes (Id, Disabled, AttributeValue) values (@id, @disabled, @attr_value) if not exists (
INSERT INTO product_search_table (id, score) VALUES ( SELECT product_id, 50 FROM cart_product WHERE product_id
INSERT IGNORE INTO table3 (id1, id2) VALUES SELECT id1, id2 FROM table1, table2; What's
INSERT INTO 'tblJobNumbers' VALUES (NULL, '3006', '05', 'THISTHS', 0, 'JOBLOCATION', '', '', 'JOBCOMMENTS', 'ES',
insert into patient_record(patient_no, patient_name, datetime_of_birth, GP_unique_no, GP_name, GP_address,complaint, treatment, doctor, datetime_treatment_started, datetime_treatment_ended) values(13, 'Sophie',
INSERT INTO `tablecity` (`City`, `Country`) VALUES ('Itajaí','Brazil'); INSERT INTO `tablecity` (`City`, `Country`) VALUES ('itajai','Brazil');
INSERT INTO rbp_users (user_id, user_name, passwd, bank_id, descr, stat, p_exp, user_type_id, email_address) VALUES (rbp_userid_seq.NEXTVAL

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.