The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Please help., Product Import Script Semi Working
NomadUK
post Mar 18 2013, 01:41 PM
Post #1





Group: Members
Posts: 3
Joined: 18-March 13
Member No.: 18,864



I have re engineered this script for a new shopping cart system. I originally paid for it when it was used on an interspire installation.

It is importing but missing lines. it imports all the values from prices and products but not descriptions. the only common denominator is the "&" symbol on the missing products descriptions.

CODE
<?php

class ProductImport{
  
   private $dbuser;
   private $dbpass;
   private $dbhost;
   private $db;
   private $importurl;
   private $product_table;
   private $product_image_table;
   private $product_category;
   private $console_string;
  
  
  
    public function __construct(){
    
    
    $this->_dbuser="blah";
    $this->_dbpass="testdev";
    $this->_dbhost="localhost";
    $this->_db="blah_testdev";
    /*$this->_importurl="http://www.domainname.co.uk/datafeed/download.asp?account=accoundn+feedid=3072";*/
    $this->_importurl="directfile.csv";
    $this->_product_table='cscart_products';
    $this->_product_descriptions='cscart_product_descriptions';
    $this->_product_prices='cscart_product_prices';
    $this->_Products_categories='cscart_products_categories';
    $this->_product_image_table='cscart_temp_product_images';
    //$this->_product_category='cscart_temp_categoryassociations';
    $this->_console_string='Collecting Product data from feed url...';
    }
    public function get_connection()
    {
        return @mysql_connect($this->_dbhost, $this->_dbuser , $this->_dbpass);
        }
        public function select_database($con)
        {
            mysql_select_db($this->_db, $con);
            }
            public function start_import_product()
            {
                $product_inserted=array();
                $csv = $this->_importurl;
                $file_handle = fopen($csv, "r");
                $i=0;
                while (!feof($file_handle) )
                {
                    $rows = fgetcsv($file_handle);
                    if($i==0){$i++; continue;
                    }
        $product_inserted[]=$this->insert_product($rows);
        
        
       }
       echo "<br>";
       echo "--------------------";
       echo "<br>";
       echo "Script Ended Sucessfully !!!!!";
       echo "<br>";
       echo "--------------------";
       echo "<br>";
       }
      
    
     public function insert_product($rows)
      {
       $connection=$this->get_connection();
       if(!$connection)
         {
          $this->stop_error('Unable to connect to Database');
         }
        
        $this->select_database($connection);
        $product_inserted=$this->insert_product_row($rows);
        
      }
      public function insert_product_row($rows)
      {
        
          
            /*---------Image data----------*/
                $image_array=array();
                $image_array['date']=time();
                $image_array['image_thumb'] = $rows[3];
                $image_array['image_large'] = $rows[2];
            /*---------Image data----------*/
                
                
            /*--------Product data---------*/
                $description=$rows[0];
                $name=$rows[1];
                $price=$rows[4];
                $status=$rows[5];
                $stock=$rows[6];
                $weight=$rows[7];
                $costprice=$rows[8];
                $categories=$rows[9];
                $productcode=$rows[10];
                $store=$rows[11];
                $prodname=$rows[12];
                $metaname=$rows[13];
                $impdate=time();
                $visibility=0;
                $catid=147;
                
                if($store=='domainname.com') {
                    $compid=1;
                }
                else
                {
                    $compid=2;
                }
                
            /*-------Refurbished Stock-------
                
                if($condition=='NEW'){
                    $showcond=0;
                }
                else
                {
                    $showcond=1;
                    $condition='Refurbished';
                }

            --------------------------------*/                                         

            /*----Discontinued Products-----*/    
                if($status=='ACTIVE'){
                    $status='A';
                }
                else
                {
                    $status='H';
                }

                
            /*--------Product Data End---------*/
            
         $already_product_id=$this->product_already_check($rows[10]);
        
         if($already_product_id==0)
         {
            if(!mysql_query("INSERT INTO ".$this->_product_table."& #40;product_code,status,company_id,list_price,amount,weight,out_of_stock_actions
) VALUES                 ('$productcode','$status','$compid','$costprice','$stock','$weight','S')"))
            {
                  echo mysql_error();
                echo "<br>";
                die;
            }
            $productid=mysql_insert_id();
            echo "Inserted the productId:".$productid." successfully !";
            echo "<br>";
         }
    /*-------Inserted the data in related tables-------*/
          if($productid){
         mysql_query("INSERT INTO `cscart_product_descriptions` & #40;product_id,lang_code,product,short_description,full_description,meta_descrip
tion) VALUES      ('$productid','EN','$prodname','$name','$description','$metaname')");
         mysql_query("INSERT INTO `cscart_product_prices` (product_id,price) VALUES  ('$productid','$price')");
         {
        
                 echo mysql_error();
                  echo "<br>";
                die;
         }}
         else {  //Update the product
        
         $update_query="UPDATE ".$this->_product_table." SET product_code = '$productcode' , list_price='$costprice' , amount='$stock' , ' WHERE productid ='".$already_product_id."'";
         $update_query="UPDATE ".$this->_product_prices." SET product_id = '$productid' , price='$price' , 'WHERE productid='".$already_product_id."'";
        
         if(!mysql_query($update_query))
          {
              //echo "<br>";
              }
              echo mysql_error();
              echo "<br>";
              echo "Updated the productId:".$already_product_id." successfully !";
              echo "<br>";
              }
              }
      
       public function product_already_check($poduct_name){
           $q="SELECT * FROM cscart_products WHERE product_code ='".$poduct_name."'";
           $res=mysql_query($q);
           $num=mysql_num_rows($res);
           if($num==0){return 0;}
           else{$row=mysql_fetch_array($res); return $row['product_id']; }
           }
        public function stop_error($Error_string){
        print("//----------------------------------//<br>");
        print("---".$Error_string."---<br>");
        print("//----------------------------------//<br>");
        exit(0);
     }


the error i get is:

Collecting Product data from feed url...
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''WHERE productid='1'' at line 1
Updated the productId:1 successfully !
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''WHERE productid='2'' at line 1
Updated the productId:2 successfully !
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''WHERE productid='3'' at line 1

Hope you can help and thanks in advance.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
NomadUK
post Mar 18 2013, 05:08 PM
Post #2





Group: Members
Posts: 3
Joined: 18-March 13
Member No.: 18,864



Fixed, Incase you were wondering.

old line

CODE
$update_query="UPDATE ".$this->_product_prices." SET product_id = '$productid' , price='$price' , 'WHERE productid='".$already_product_id."'";


New line

CODE
$update_query="UPDATE ".$this->_product_prices." SET price = '$price' WHERE product_id = ".$already_product_id;


I also had to remove updating the product ID, was getting a duplicate field error
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 28th March 2024 - 08:03 AM