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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:04:26+00:00 2026-06-12T18:04:26+00:00

I’m using PHP and I need to insert a new record in a MySQL

  • 0

I’m using PHP and I need to insert a new record in a MySQL DB or if it exists simply update it. I read several answers where they propose to use REPLACE or INSERT...ON DUPLICATE KEY UPDATE, however if I well understood, these two option imply the use of unique fields or primary keys (that are unique). In my case the MySQL table has values similar to this:

Timestamp            | Query1   | Query2   | Result
-------------------------------------------------------- 
2012-10-13 08:15:27  | American | Men      | here result

2012-10-13 08:15:23  | American | Men      | result2

2012-10-13 08:15:27  | American | Women    | other result

2012-10-13 08:15:27  | German   | Men      | here result

Therefore I cannot have primary keys or univoque fields since “query1”, “query2” and “Result” can have the same values (e.g. I can have several records with more fields containing “American” (query1) and “Men” (query2) and different results.

At the moment in PHP I’m using:

INSERT INTO results (Timestamp, Query1, Query2, Result) VALUES ('$current_timestamp', '$nationality', '$gender', '')

Which just append all the records and create a huge DB. But what I want to achieve is adding a new record just if the entire combination query1&query2&result doesn’t exist. Otherwise I just want to update the “timestamp” field.

For instance if my PHP script produces the following data:

Timestamp            | Query1   | Query2   | Result
2012-10-13 08:15:23  | American | Men      | result2

and the database already contains this exact combination of “American”&”Men”&”result2” then just the field “timestamp” is updated. Otherwise, if the combination is different (e.g. “American” “Men” “result3) a new record is added to the table.

Thank you in advance for your help

  • 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-12T18:04:27+00:00Added an answer on June 12, 2026 at 6:04 pm

    Even absent a single PRIMARY KEY column like a typical AUTO_INCREMENT integer, if those three columns are meant to be a unique combination you ought to define them as a composite key . This will not only preserve uniqueness but also enforce indexing on them as a unit.

    CREATE TABLE results (
      `Timestamp` DATETIME NOT NULL,
      `Query1` VARCHAR() NOT NULL,
      `Query2` VARCHAR() NOT NULL,
      `Result` VARCHAR() NOT NULL,
      /* Composite key across three columns */
      PRIMARY KEY (`Query1`,`Query2`,`Result`)
    );
    

    You may then use ON DUPLICATE KEY UPDATE when inserting to abort insertion on key violations and update the timestamp instead.

    INSERT INTO results (
       Timestamp, 
       Query1, 
       Query2, 
       Result
    ) VALUES (
       '$current_timestamp', /* see note below about NOW() */
       '$nationality', 
       '$gender',
       ''
    ) ON DUPLICATE KEY UPDATE `Timestamp` = '$current_timestamp';
    

    Note: We assume the PHP variables have been properly escaped already.

    Note 2: If the value of $current_timestamp is indeed the current timestamp and not some stored value, I recommend using MySQL’s NOW() function rather than pass in a PHP variable.

    ON DUPLICATE KEY UPDATE `Timestamp` = NOW()
    

    Same goes for the $current_timestamp in the VALUES() list…

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.