0

I am creating a custom module, but it's not inserting data in table. While it's show "successful" message Here my observe.php code is:

   $write1 = Mage::getSingleton("core/resource")->getConnection("core_write");
                //$query_meter = "insert into  catalog_customer_product_meterreading (customer_id, product_id,meter_key ) values ($cutomer_id, $product_id,'$serial_key')";
                $query_meter =  "INSERT INTO `catalog_customer_product_meterreading` (`customer_id`, `product_id`, `meter_key`) VALUES ('2', '2', '334')";

                   // print_r($write1) ;

                    if($write1->query($query_meter)){
                        echo "Data successfully inserted.";
                        }

My Custom table is:

CREATE TABLE IF NOT EXISTS `catalog_customer_product_meterreading` (
  `customer_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Customer ID',
  `product_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Product ID',
  `meter_key` varchar(100) NOT NULL COMMENT 'Meter Reading with #',
  PRIMARY KEY (`customer_id`,`product_id`,`meter_key`),
  KEY `IDX_CATALOG_CATEGORY_PRODUCT_PRODUCT_ID` (`product_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='Meter Reading To Catalog Product   Table';



ALTER TABLE `catalog_customer_product_meterreading`
 ADD CONSTRAINT `catalog_customer_product_meterreading_ibfk_2` FOREIGN KEY (`customer_id`) REFERENCES `customer_entity` (`entity_id`)ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `catalog_customer_product_meterreading_ibfk_1` FOREIGN KEY (`product_id`) REFERENCES `catalog_product_entity` (`entity_id`)ON DELETE CASCADE ON UPDATE CASCADE;

Please suggests me where my code is wrong. If i copy/paste query directly in database it run fine.

Thanks in advance

user1493023
  • 99
  • 1
  • 2
  • 13
  • Please be absolutely certain that your data is [properly escaped](http://bobby-tables.com/php) because it doesn't look like you've done that here. – tadman Jul 22 '13 at 13:47
  • please describe it more, so that i can resolve it. – user1493023 Jul 24 '13 at 05:27
  • Things like `$customer_id` should not be directly inside your query because of [SQL injection concerns](http://bobby-tables.com/). The Magento library has [ways of properly escaping this](http://stackoverflow.com/questions/3575160/using-magento-methods-to-write-insert-queries-with-care-for-sql-injection) that you should be using. – tadman Jul 24 '13 at 14:14

0 Answers0