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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:39:18+00:00 2026-05-11T16:39:18+00:00

I am working in PHP. Please what’s the proper way of inserting new records

  • 0

I am working in PHP.

Please what’s the proper way of inserting new records into the DB, which has unique field.
I am inserting lot of records in a batch and I just want the new ones to be inserted and I don’t want any error for the duplicate entry.

Is there only way to first make a SELECT and to see if the entry is already there before the INSERT – and to INSERT only when SELECT returns no records? I hope not.

I would like to somehow tell MySQL to ignore these inserts without any error.

Thank you

  • 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-11T16:39:18+00:00Added an answer on May 11, 2026 at 4:39 pm

    You can use INSERT… IGNORE syntax if you want to take no action when there’s a duplicate record.

    You can use REPLACE INTO syntax if you want to overwrite an old record with a new one with the same key.

    Or, you can use INSERT… ON DUPLICATE KEY UPDATE syntax if you want to perform an update to the record instead when you encounter a duplicate.

    Edit: Thought I’d add some examples.

    Examples

    Say you have a table named tbl with two columns, id and value. There is one entry, id=1 and value=1. If you run the following statements:

    REPLACE INTO tbl VALUES(1,50);
    

    You still have one record, with id=1 value=50. Note that the whole record was DELETED first however, and then re-inserted. Then:

    INSERT IGNORE INTO tbl VALUES (1,10);
    

    The operation executes successfully, but nothing is inserted. You still have id=1 and value=50. Finally:

    INSERT INTO tbl VALUES (1,200) ON DUPLICATE KEY UPDATE value=200;
    

    You now have a single record with id=1 and value=200.

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

Sidebar

Related Questions

I am working with php and want to have a page that has a
HI i have a working php based pagination which works fine, but when i
I'm working with PHP and MySQL. Before we start: I'm aware of SQL-Injection: Please
I am working with a PHP library to generate Excel files which consists of
I am pretty new to PHP so please be kind :). I have been
Hi All I have a project working in PHP which is divided in an
Please forgive the extreme-beginner style of coding. I'm working with PHP and JSON strings
I am working in php in which i am using mysql. This is my
I am working in php. I designed a function which calculates the distance between
i am working in php. i have a table EVENT. in which there are

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.