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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:19:25+00:00 2026-05-15T08:19:25+00:00

I need to insert records, but if the records exist do a replace instead.

  • 0

I need to insert records, but if the records exist do a replace instead. Here is what I am currently using:

  $sessionDate = date("Y-m-d H:i:s");
  foreach($tmpVP as $sessionVP) {
    $res = mysql_query("INSERT INTO sessions
                      (sessionID,sessionDate,sessionVS,sessionVP)
                      VALUES('$sessionID','$sessionDate','$sessionVS',
                      '$sessionVP')") ;
  }

What I really need is to update any records matching sessionID, sessionVS, and sessionVP and insert new records that don’t match.

EDIT:

Table definition

CREATE TABLE `sessions` (
 `ID` bigint(20) NOT NULL auto_increment,
 `sessionID` varchar(36) NOT NULL,
 `sessionDate` datetime NOT NULL,
 `sessionUser` bigint(20) NOT NULL,
 `sessionVS` varchar(255) NOT NULL,
 `sessionVP` bigint(20) NOT NULL,
 `reserved` int(11) NOT NULL,
 PRIMARY KEY  (`ID`),
 KEY `ID` (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=88 DEFAULT CHARSET=utf8

sessionID, sessionVP, and sessionVS are not unique. Sample:

sessionID                  sessionDate          sessionUser sessionVS sessionVP 
0t1m58q9ktejuhqlrjqglcoia0 2010-06-20 09:20:53  0           111111    144268736
0t1m58q9ktejuhqlrjqglcoia0 2010-06-20 09:20:53  0           111111    144268819
0t1m58q9ktejuhqlrjqglcoia0 2010-06-20 09:20:53  0           111111    144268880

So, if I have a new record with 0t1m58q9ktejuhqlrjqglcoia0, 111111, and `144268880′ I need to update row #3 instead of adding row #4.

  • 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-15T08:19:26+00:00Added an answer on May 15, 2026 at 8:19 am

    Firstly you should add a unique index on (sessionID, sessionVP, sessionVS). You can do this using the following command:

    CREATE UNIQUE INDEX ux_sessions_sessionid_sessionvs_sessionvp
    ON sessions (sessionID, sessionVS, sessionVP)
    

    Then there are two relatively simple ways to insert or update in MySQL. The first is to use ON DUPLICATE UPDATE:

    INSERT INTO sessions
    (sessionID,sessionDate,sessionVS,sessionVP)
    VALUES
    ('$sessionID', '$sessionDate', '$sessionVS', '$sessionVP')
    ON DUPLICATE KEY UPDATE sessionDate = '$sessionDate'
    

    There other is to use REPLACE:

    REPLACE INTO sessions
    (sessionID,sessionDate,sessionVS,sessionVP)
    VALUES
    ('$sessionID', '$sessionDate', '$sessionVS', '$sessionVP')
    

    The second is slightly more concise, but has the disadvantage that it internally causes a delete followed by an insert.

    There are also a few other issues:

    • You don’t need both a primary key index and an ordinary index on ID. Remove the ordinary index as it is redundant.

    • You may have an SQL vulnerability. If you have not already validated the input you might want to consider protecting yourself by using mysql_real_escape_string or intval as appropriate. Alternatively you could look at using query parameters.

    • You are not checking for error conditions. Consider using trigger_error so that if your query has an error you can see what the error is. Seeing the error message can save you a lot of time debugging.

      mysql_query("...") or trigger_error(mysql_error());
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 475k
  • Answers 475k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using Connection#getTransactionLevel() on the underlying Connection should work. Here is… May 16, 2026 at 4:44 am
  • Editorial Team
    Editorial Team added an answer Reading files in C is usually a bit messy. The… May 16, 2026 at 4:44 am
  • Editorial Team
    Editorial Team added an answer I can see two initial approaches, namely define a class… May 16, 2026 at 4:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.