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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:35:28+00:00 2026-05-28T03:35:28+00:00

Using PHP I’m inserting a record in a MySQL database. If the record is

  • 0

Using PHP I’m inserting a record in a MySQL database. If the record is new I get the insert_id but if it already exists I can’t get the id using a single query.

Both of the following queries allow me to add the record or skip if it already exists:

INSERT INTO `emails` (`email`) VALUE ('abc@example.com') ON DUPLICATE KEY UPDATE `id`=`id`

INSERT IGNORE INTO `emails` (`email`) VALUE ('abc@example.com')

What is the right way to get the right result in both cases?

EDIT: Here’s my table syntax:

CREATE TABLE `emails` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 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-28T03:35:29+00:00Added an answer on May 28, 2026 at 3:35 am

    I suspect that it’s the value of LAST_INSERT_ID that ends up in insert_id.

    Quote from the MySQL manual;

    To make LAST_INSERT_ID() meaningful for updates, insert rows as follows:

    INSERT INTO table (a,b,c) VALUES (1,2,3)
      ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id), c=3;
    

    Source: http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

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

Sidebar

Related Questions

Using PHP, I am building an application that is MySQL database resource heavy, but
Using PHP, I can convert MySQL data or static table data to csv, Excel,
Using PHP, I am trying to delete a record, but I want to check
Using PHP how can I get week's start date and end date from any
Using PHP and MySQL I'm trying to get all the distinct values out the
Using PHP (other languages, using common built-ins are welcome), how can I get a
Using PHP and MySQL, I have a forum system I'm trying to build. What
using php and mysql I have two tables, a users table and a profiles
Using PHP and MySQL, I am integrating a site with a wiki based on
Using PHP's Image and GD functions you can use the following method to finally

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.