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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:29:08+00:00 2026-06-01T19:29:08+00:00

![1062 Duplicate entry][2] In Magento’s Product creation code, I add my own custom code

  • 0

![1062 Duplicate entry][2]
In Magento’s Product creation code, I add my own custom code in /htdocs/magento/app/code/core/Mage/Adminhtml/controllers/Catalog/ProductController.php I have make external mysql connection & use that for inserting product id & sku in custom table.(I know this is not recommended way but just for testing purpose I am doing this.)

My code-:

public function saveAction()
{
        $storeId        = $this->getRequest()->getParam('store');
        $redirectBack   = $this->getRequest()->getParam('back', false);
        $productId      = $this->getRequest()->getParam('id');
        $isEdit         = (int)($this->getRequest()->getParam('id') != null);

        $data = $this->getRequest()->getPost();
        if ($data) {
            if (!isset($data['product']['stock_data']['use_config_manage_stock'])) {
                $data['product']['stock_data']['use_config_manage_stock'] = 0;
            }
            $product = $this->_initProductSave();

            try {
                $product->save();
                $productId = $product->getId();
//-------My code start---------------------------------
     $db_name = "magento";
             $con = mysql_connect  ("www.xyz.com", "magento", "password");
            If(!$con)
    {
        die('Could not connect: ' . mysql_error());
        mysql_close($con);
    }
    $seldb = mysql_select_db($db_name, $con);

    $query_fetch = "SELECT cpe.entity_id, cpe.sku FROM catalog_product_entity cpe
               WHERE cpe.entity_id = ".$productId;

    $result_query_fetch = mysql_query($query_fetch);
    while($row = mysql_fetch_array($result_query_fetch))
    {
        $entity_id = ($row["entity_id"]);
        $sku = ($row["sku"]);           
        $result_fetch = "$entity_id".",'".$sku."'";
    }

         $query_insert = "INSERT into product_creation(entity_id,sku,creation_date) VALUES(".$result_fetch.", NOW())";
    $result_insert = mysql_query($query_insert);

    mysql_close($con);
  //--------------------------My code End-----------------------------------------

                /**
                 * Do copying data to stores
                 */
                if (isset($data['copy_to_stores'])) {
                    foreach ($data['copy_to_stores'] as $storeTo=>$storeFrom) {
                        $newProduct = Mage::getModel('catalog/product')
                            ->setStoreId($storeFrom)
                            ->load($productId)
                            ->setStoreId($storeTo)
                            ->save();
                    }
                }


                Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId);

                $this->_getSession()->addSuccess($this->__('The product has been saved.'));


            }
            catch (Mage_Core_Exception $e) {
                $this->_getSession()->addError($e->getMessage())
                    ->setProductData($data);
                $redirectBack = true;
            }
            catch (Exception $e) {
                Mage::logException($e);
                $this->_getSession()->addError($e->getMessage());
                $redirectBack = true;
            }
        }

        if ($redirectBack) {
            $this->_redirect('*/*/edit', array(
                'id'    => $productId,
                '_current'=>true
            ));
        }
        else if($this->getRequest()->getParam('popup')) {
            $this->_redirect('*/*/created', array(
                '_current'   => true,
                'id'         => $productId,
                'edit'       => $isEdit
            ));
        }
        else {
            $this->_redirect('*/*/', array('store'=>$storeId));
        }
    }

So from this code, product data is get inserted in both tables but I am getting error as

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘1949-1’ for key ‘IDX_STOCK_PRODUCT’

Can anybody plz help me resolve this issue…

Solution I tried-:

-remove /var/cache & /var/session

-clear browser cache & cookies

-In the app/etc/config.xml, change this

SET NAMES utf8
to this

SET NAMES utf8; SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0;

Still getting same error…Or plz tell me what should I change in my code?

P.S.- I know that this is not recommended way as I am changing code of core files & also using external connection rather than using ZEND connection…but it is just for testing purpose…

Is there any solution for this issue?

plz help me…

  • 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-01T19:29:09+00:00Added an answer on June 1, 2026 at 7:29 pm

    The error is quite clear on what your problem is:
    You have a unique key constraint IDX_STOCK_PRODUCT and there already is a record with value ‘1949-1’ in whatever column that key covers.

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

Sidebar

Related Questions

I'm getting the following error while executing database query: Error Number: 1062 Duplicate entry
Alright, so I'm getting the error: #1062 - Duplicate entry '0' for key 'PRIMARY'
Is it recommended to add the slave-skip-errors = 1062 option into the my.cnf file?
While creating product, at the last step after retrieving for a time, Magento gives
I'm stuck on a mySQL query using ON DUPLICATE KEY UPDATE. I'm getting the
Everytime I try restoring the dump, I get : ERROR 1062 (23000) at line
After upgrading magento from 1.4.0.1 to 1.5.1.0 , i'm not able to submit an
So when trying to add an autoincrement to a field it comes up with
I'm testing out an app (hopefully to run on heroku, but am having issues
I'm trying to figure out why I am getting this error when my Magento

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.