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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:08:54+00:00 2026-05-31T22:08:54+00:00

I am stuck with a peculiar issue here. I have a script that basically

  • 0

I am stuck with a peculiar issue here. I have a script that basically imports a CSV file into a database using fgetcsv() in php. There is no problem in doing this at all and I am able to update old entries as well using MySQL syntax ON DUPLICATE KEY UPDATE (I am in no way a MySQL expert, hence me asking here).

Here is that part of the code:

$handle = fopen($file,"r");
fgetcsv($handle, 1000, ",");//skip first row since they are headers
while(($fileop = fgetcsv($handle, 1000, ",")) !== false) //read line by line into $fileop
{
  //read array values into vars
  $item1 = $fileop[0];
  $item2 = $fileop[1];
  $key = $fileop[2];
  // and a couple more

  // now INSERT / UPDATE data in MySQL table
  $sql = mysql_query("INSERT INTO table (item1,item2,key) 
    VALUES ('$item1','$item2','$key') 
    ON DUPLICATE KEY UPDATE item1='$item1',item2='$item2'");

}

This all works fine. What I am stuck with is the fact that some entries may have been removed from the actual CSV (as in the key may no longer be existant). What I would like to do is remove the entries from the MySQL table that are no longer present in the CSV.

Meaning if $key is gone from CSV also remove that row in the database table. I suppose I would do it before I run the Insert / Update query on the MySQL table?

I would appreciate any help guys.

  • 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-31T22:08:55+00:00Added an answer on May 31, 2026 at 10:08 pm

    Just keep an account of your keys.

    Save every $key in an array in your while, and in the end run a query that says

    DELETE FROM tabel WHERE key NOT IN (listofcommaseparatedkeysgoeshere)
    

    $arrayThatYouNeedToTest = array();
    $handle = fopen($file,"r");
    fgetcsv($handle, 1000, ",");//skip first row since they are headers
    while(($fileop = fgetcsv($handle, 1000, ",")) !== false) //read line by line into $fileop
    {
      //read array values into vars
      $item1 = $fileop[0];
      $item2 = $fileop[1];
      $key = $fileop[2];
      // and a couple more
    
      // now INSERT / UPDATE data in MySQL table
      $sql = mysql_query("INSERT INTO table (item1,item2,key) 
        VALUES ('$item1','$item2','$key') 
        ON DUPLICATE KEY UPDATE item1='$item1',item2='$item2'");
    
       $arrayThatYouNeedToTest[] = $key;    
    
    }
    
    $stringThatYouNeedToInspect = implode(",",$arrayThatYouNeedToTest);
    $queryYouREALLYneedToCheckFirst = "DELETE FROM tabel WHERE key NOT IN  (".$stringThatYouNeedToInspect.")";
    
    //$result = mysql_query($queryYouREALLYneedToCheckFirst);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Stuck with this particular issue. I have code that get lots of pages from
Being stuck with a legacy database schema that no longer reflects your data model
Kinda stuck here... I have an application with lets say 5000 rows of data
Stuck again. :( I have the following code crammed into a procedure invoked when
So very peculiar spring httpinvoker issue going on. I have a soap web service
im stuck here and i don't know how to fix it. I have a
Stuck here trying to initialize an array (c#) using a loop. The number of
Stuck trying to figure out the best algorithm here. I have this simple function
I've become stuck with an issue that I am sure is on- the- nose
I am running into a peculiar issue with an app which has multiple Activities.

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.