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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:55:50+00:00 2026-05-25T15:55:50+00:00

I have a PHP file that reads in a CSV file and stores the

  • 0

I have a PHP file that reads in a CSV file and stores the values into an SQL table.

The SQL table has 4 columns

 id - primary key (just auto incremented integer)
 name - text value of an object name
 type - an integer value representing the type of object
 active - a bool to set if the object is active

The problem is the CSV file doesn’t store the id, just the name and type.

I want to do the following and wondered what the best way to do this is with the least amount of queries.

set active to 0 for every entry in the table

For each entry in the CSV
if (name and type already exists in table)
  set active to 1 
else 
   add new entry to the table

return the id for the existing or new entry

any help would be greatly appreciated

Thanks

  • just a note, it’s just the mysql queries i’m after, not php code etc…
  • 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-25T15:55:50+00:00Added an answer on May 25, 2026 at 3:55 pm

    Assuming you are using mysql:

    First you have to create “unique” index on 2 fields: name and type (it’s one index not two)

    Then insert your data using this query:

    INSERT INTO `table` (`name`,`type`,`active`) VALUES ($name,$type,0)
      ON DUPLICATE KEY UPDATE active = 1
    

    In order to get ID you just query it (the smart way – mysql_insert_id() doesn’t work with INSERT UPDATE)

    SELECT id FROM `table` WHERE `name`= '$name' AND `type` = '$type'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following PHP that reads in a CSV file and outputs rows/cells
I have a php file that reads data from my mysqldb as a json
I have read that when including a php file that using absolute paths has
I have a PHP script that reads in data from an XML file, returns
I have a php script that reads an XML file and modifies it with
I have a php script that reads one file through http(the file is on
I have a piece of php code that reads values from a database resultset,
I have a PHP file that contains a lot of if and else statements
I have a php file that contains a form (which contains 2 input boxes
I have a PHP file that I need it to detect it's directory it's

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.