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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:07:35+00:00 2026-06-01T09:07:35+00:00

I am reading .csv file through PHP using fgetcsv(). Now I am able to

  • 0

I am reading .csv file through PHP using fgetcsv(). Now I am able to fetch I mean read data from .csv file.
Then I need to check SKU column with my fetch result & accordingly have to perform either Insertion Or Updation.
Suppose SKU is already present there, then I have to update my row in table else I need to insert new record.
I have write following code…Plz check n tell me where I am doing mistake-:

<?php
$row = 1; 
if (($handle = fopen("localhost/data/htdocs/magento/var/import/Price.csv", "r")) !== FALSE) 
{     
    while (($data = fgetcsv($handle, 8000, ",")) !== FALSE) 
    {         
        $num = count($data);         
        echo "<p> $num fields in line $row: <br /></p>\n";           
        for ($c=0; $c < $num; $c++) 
        {             
            $temp = $data;             
            $string = implode(";", $temp);           
        }
            $pieces = explode(";", $string);             
            $col1 = $pieces[0];  
            $col2 = $pieces[1];
            $col3 = $pieces[2];      
            $col4 = $pieces[3];             

            $db_name = "magento";
                        $con = mysql_connect("localhost", "magento", "password");
                     If (!$con)
            {
                die('Could not connect: ' . mysql_error());
                mysql_close($con);
            }
            $seldb = mysql_select_db($db_name, $con);

            $query_fetch = "SELECT `sku` from `imoprt_prices`";
            $result_fetch = mysql_query($query_fetch);
            $num_rows = mysql_num_rows($result_fetch); 

            for($i = 0; $i < $num_rows; $i++) 
            { 
                $value = mysql_result($result_fetch, i, 'sku');
                if(strcmp('".$value."', '".$col2."') == 0)
                {               
                    $flag = 1;
                    break;
                }
                else
                {
                    $flag = 0;
                        break;
                }
            }
                if($flag == 1)
                {
                    $query_upadte = "(UPDATE imoprt_prices SET customer_id= '".$col1."', sku ='".$col2."', price= '".$col3."', website= '".$col4."'
                    )";
                    mysql_query($query_upadte);
                        $row++;         
                }
                if($flag == 0)
                {
                    mysql_query("INSERT INTO `imoprt_prices`(`customer_id`,`sku`,`price`,`website`) VALUES('".$col1."','".$col2."','".$col3."','".$col4."')");
                    $row++;   
                } 

    } 
} 
?>
  • 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-01T09:07:37+00:00Added an answer on June 1, 2026 at 9:07 am

    If you have an actual UNIQUE index on your imoprt_prices table, you can use the ON DUPLICATE KEY UPDATE syntax and simplify your code a bit to something similar to; (note, can’t test, so see as pseudo code)

    $db_name = "magento";
    $con = mysql_connect("localhost", "magento", "password") or die(mysql_error());
    $seldb = mysql_select_db($db_name, $con);
    
    if (($handle = fopen("localhost/data/htdocs/magento/var/import/Price.csv", "r")) !== FALSE) 
    {     
        while (($data = fgetcsv($handle, 8000, ",")) !== FALSE) 
        {
            $col1 = $pieces[0];  
            $col2 = $pieces[1];
            $col3 = $pieces[2];      
            $col4 = $pieces[3]; 
    
            $query_upadte = "INSERT INTO imoprt_prices (customer_id, sku, price, website) ".
                "VALUES('".$col1."','".$col2."','".$col3."','".$col4."') ".
                "ON DUPLICATE KEY UPDATE customer_id='".$col1."', price='".$col3.
                "',website='".$col4."'";
            mysql_query($query_upadte);
        }
    }
    

    You may also want to either mysql_real_escape_string() or use parameterized queries to make sure there’s no ' in your inserted values though. That is always a danger with building sql strings like this.

    Simple demo here.

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

Sidebar

Related Questions

I have .zip file which contain csv data. I am reading .zip file using
I'm using BufferedReader to read a .csv file. I have no problem reading the
I am using the following method for reading Csv file content: /// <summary> ///
I have a System.Data.DataTable which is populated by reading a CSV file which sets
Im having trouble reading from a CSV file final String DELIMITER = ,; Scanner
I'm trying to call the LumenWorks .csv file reading library from a C++/CLI application
i am reading a csv file into a data: def get_file(start_file): #opens original file,
All, I'm reading a csv file and adding the data to a MySQL DB
I have been writing an implementation for reading a .csv data file into C#
I'm trying to load data from an external csv file that needs to be

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.