Zen Cart Logo

CSV Import

Views: 1,047

Results 1 to 1 of 1
31 Dec 2011, 18:50
#1
bn17311 avatar

bn17311

Zen Follower

Join Date:
Apr 2010
Posts:
319
Plugin Contributions:
0

CSV Import

Hi,
i am using this script i found on this forum and it is doing exactly what i need but wondering is it secure or should it be done different to make it secure

thanks for any help
Bryan

<?php 

// Connect to MySQL 
mysql_connect("localhost", "db_user", "password") or die(mysql_error()); 
mysql_select_db("db_name") or die(mysql_error()); 

#if first row of csv file is headings set $row to 1. 
$row = 0; 
#database primary table 
$table_to_update = "products"; 

#get the csv file 
$handle = fopen("your_data.csv", "r"); 

#go through the csv file and print each row with fields to the screen. 
#and import them into the database updating only the price and quantity  
while (($data = fgetcsv($handle, 100000, ",")) !== FALSE) { 
    $num = count($data); 
    echo "<p> $num fields in line $row: <br /></p>\n"; 
    $row++; 
    for ($c=0; $c < $num; $c++) { 
        if ($c = 1) { 
        $product_model = $data[($c - 1)]; 
        echo $product_model . " SKU Assigned <br />\n"; 
        } 
        if ($c = 2) { 
        $product_price = $data[($c - 1)]; 
        mysql_query("UPDATE $table_to_update SET products_price='$product_price' WHERE products_model='$product_model'")  
        or die(mysql_error());   
        echo $product_price . " Imported for row $row in product $product_model<br />\n"; 
        } 
        if ($c = 3) { 
        $product_quantity = $data[($c - 1)]; 
        mysql_query("UPDATE $table_to_update SET products_quantity='$product_quantity' WHERE products_model='$product_model'")  
        or die(mysql_error());  
        echo $product_quantity . " Imported for row $row in product $product_model <br />\n"; 
        } 
        // would have to add an additional  if statement for each field being updated and know the order of the fields from your csv file 
        //echo $data[$c] . "Imported <br />\n"; 
         
         
    } 
} 

fclose($handle); 
echo "<h1>Update Complete.</h1>"; 
?>