I want to insert some information of customer into all customer tables from outside form but I am not able to insert it and I have also used following code :

$address = Mage::getModel("customer/address");
    //->setRegionId('1') //state/province, only needed if the country is USA
    ->setCity('Beverly Hills')

catch (Exception $e) {

But I am getting some error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`magento`.`customer_address_entity`, CONSTRAINT `FK_CUSTOMER_ADDRESS_ENTITY_PARENT_ID_CUSTOMER_ENTITY_ENTITY_ID` FOREIGN KEY (`parent_id`) REFERENCES `customer_entity` (`entity_id`) ON DELETE )' in C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement\Pdo.php:228
Stack trace:
#0 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement\Pdo.php(228): PDOStatement->execute(Array)
#1 C:\wamp\www\magento\mmm\magento\lib\Varien\Db\Statement\Pdo\Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array) 
#2 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement.php(300): Varien_Db_Statement_Pdo_Mysql->_execute(Array) 
#3 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Adapter\Abstract.php(479): Zend_Db_Statement->execute(Array) 
#4 C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Adapter\Pdo\Abstract.php(238): Zend_Db_Adapter_Abstract->query('INSERT INTO `cu...', Array) in C:\wamp\www\magento\mmm\magento\lib\Zend\Db\Statement\Pdo.php on line 234

Please help me and suggest me how to solve it so that I can insert some information of customer into database from outside form. Thanks

  • please check customer id exit in system and also check customer website
    – Amit Bera
    Commented Feb 24, 2015 at 9:43
  • Your problem is, that you want to save a customer address without customer_id Commented Feb 24, 2015 at 11:30

1 Answer 1


I have solved this problem and I used following code :

require PATH_TO_MAGENTO_FOLDER . '/app/Mage.php';

$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");  //If you want to insert only customer information into DB then use it.
$customer   ->setWebsiteId($websiteId)
        ->setGroupId(1) // if you want to change group then used GroupId 1 or 2 or 3 
        ->setEmail('[email protected]')

$address = Mage::getModel("customer/address"); // If you also want to insert all address into DB then use it.
    ->setRegionId('1') //state/province, only needed if the country is USA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.