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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:40:24+00:00 2026-05-25T21:40:24+00:00

I’ve seen the following (using the VALUES option): $query = INSERT INTO $table (column-1,

  • 0

I’ve seen the following (using the VALUES option):

$query = "INSERT INTO $table (column-1, column-2, column-3) VALUES ('value-1', 'value-2', 'value-3') ON DUPLICATE KEY UPDATE SET column1 = value1, column2 = value2, column3 = value3, ID=LAST_INSERT_ID(ID)"; 

… but I can’t figure how to add ON DUPLICATE KEY UPDATE to what I’m using:

$query = "INSERT INTO $table SET
    column-1 ='value-1',
    column-2 ='value-2',
    column-3 ='value-3'
";

e.g.:, pseudo-code

$query = "INSERT INTO $table SET
    column-1 ='value-1',
    column-2 ='value-2',
    column-3 ='value-3'
    ON DUPLICATE KEY UPDATE SET
    column1 = value1,
    column2 = value2,
    column3 = value3,
    $id=LAST_INSERT_ID(id)"; 
    $my_id = mysql_insert_id();
";

I would find the latter easier to read. Would appreciate clarification, didn’t find an example in the manual.

cheers

  • 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-25T21:40:25+00:00Added an answer on May 25, 2026 at 9:40 pm

    I’ve used ON DUPLICATE KEY UPDATE a lot. For some situations it’s non-standard SQL extension that’s really worth using.

    First, you need to make sure you have a unique key constraint in place. The ON DUPLICATE KEY UPDATE function only kicks in if there would’ve been a unique key violation.

    Here’s a commonly used format:

     $query = "INSERT INTO $table (column1, column2, column3)
     VALUES ('value-1', 'value-2', 'value-3')
     ON DUPLICATE KEY UPDATE
     column1 = values(column1),
     column2 = values(column2),
     column3 = values(column3);"
    

    column1 = values(column1) means “Update column1 with the value that would have been inserted if the query hadn’t hit the duplicate key violation.” In other words, it just means update column1 to what it would’ve been had the insert worked.

    Looking at this code, it doesn’t seem correct that you’re updating all three of the columns you’re trying to insert. Which of the columns has a unique constraint on it?

    EDIT: Modify based on ‘SET’ format of mysql insert statement per the question from the OP.

    Basically to use ON DUPLICATE KEY UPDATE, you just write the insert statement as you normally would, but add the ON DUPLICATE KEY UPDATE clause tacked onto the end. I believe it should work like this:

    INSERT INTO $table 
        set column1 = 'value-1',
            column2 = 'value-2',
            column3 = 'value-3'
    ON DUPLICATE KEY UPDATE
        column1 = values(column1),
        column2 = values(column2),
        column3 = values(column3);
    

    Again, one of the columns you’re inserting has to have a unique index (or a combination of the columns). That can be because one of them is the primary key or because there is a unique index on the table.

    • 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 using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
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

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.