My CSV,

My MySQL table.

PHP script :
<?php
require_once 'app/Mage.php';
umask("0");
Mage::app();
ini_set('display_errors',"1");
ini_set('display_startup_errors', "1");
error_reporting(E_ALL);
$connection=Mage::getSingleton('core/resource')->getConnection('core_write');
if (($handle = fopen("inputs.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, '1000', ",")) !== FALSE)
{
$sql = "insert into". Mage::getSingleton('core/resource')->getTableName('catalog_category_product').
"('category_id','product_id','position')". "values".('.$data["0"].','.$data["1"].','.$data["2"].')" ;
$connection->query($sql, array($data['0'], $data['1'],$data['2']));
}
fclose($handle);
}
?>
How to insert values from csv using PHP script?
What happens when you run your code?
In this bit:
$sql = "insert into". Mage::getSingleton('core/resource')->getTableName('catalog_category_product').
"('category_id','product_id','position')". "values".('.$data["0"].','.$data["1"].','.$data["2"].')" ;
I suspect you don’t need quotes around
'.$data["0"].','.$data["1"].','.$data["2"].')" ;
the numbers in this part.
You should look at prepared statements, you’d be able to get rid of all those confusing quotations.
How does your CSV file relate to the columns in your database table? I can’t see a link.
Solved.
<?php
require_once 'app/Mage.php';
umask("0");
Mage::app();
ini_set('display_errors',"1");
ini_set('display_startup_errors', "1");
error_reporting(E_ALL);
$connection=Mage::getSingleton('core/resource')->getConnection('core_write');
if (($handle = fopen("inputs.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, '1000', ",")) !== FALSE)
{
$sql =" insert into ". Mage::getSingleton('core/resource')->getTableName('catalog_category_product').
" (category_id,product_id,position) ". " values "." (" .$data['0']. "," .$data['1']. "," .$data['2']. ") " ;
echo $sql;
$connection->query($sql, array($data['0'], $data['1'],$data['2']));
}
fclose($handle);
}
?>
system
Closed
6
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.