I have a issue with inserting data into the Magento mysql database.
I wanted to create a new column (howyouheard) in the existing database in Magento. The column is to take the values from customers about how they heard about my store. I created a front end HTML form which posts data using a php function. Find the function which does that below
public function createPostAction()
{
$session = $this->_getSession();
if ($session->isLoggedIn()) {
$this->_redirect('*/*/');
return;
}
$session->setEscapeMessages(true); // prevent XSS injection in user input
if ($this->getRequest()->isPost()) {
$errors = array();
if (!$customer = Mage::registry('current_customer')) {
$customer = Mage::getModel('customer/customer')->setId(null);
}
$data = $this->_filterPostData($this->getRequest()->getPost());
foreach (Mage::getConfig()->getFieldset('customer_account') as $code=>$node) {
if ($node->is('create') && isset($data[$code])) {
if ($code == 'email') {
$data[$code] = trim($data[$code]);
}
$customer->setData($code, trim($data[$code]));
$customer->setData('howyouheard', trim($data['howyouheard']));
}
}
if ($this->getRequest()->getParam('is_subscribed', false)) {
$customer->setIsSubscribed(1);
}
/**
* Initialize customer group id
*/
$customer->getGroupId();
if ($this->getRequest()->getPost('create_address')) {
$address = Mage::getModel('customer/address')
->setData($this->getRequest()->getPost())
->setIsDefaultBilling($this->getRequest()->getParam('default_billing', false))
->setIsDefaultShipping($this->getRequest()->getParam('default_shipping', false))
->setId(null);
$customer->addAddress($address);
$errors = $address->validate();
if (!is_array($errors)) {
$errors = array();
}
}
try {
$validationCustomer = $customer->validate();
if (is_array($validationCustomer)) {
$errors = array_merge($validationCustomer, $errors);
}
$validationResult = count($errors) == 0;
if (true === $validationResult) {
$customer->save();
Mage::log($customer, null, 'howyouheard_customer.log');
Mage::dispatchEvent('customer_register_success',
array('account_controller' => $this, 'customer' => $customer)
);
if ($customer->isConfirmationRequired()) {
$customer->sendNewAccountEmail('confirmation', $session->getBeforeAuthUrl());
$session->addSuccess($this->__('Account confirmation is required. Please, check your email for the confirmation link. To resend the confirmation email please <a href="%s">click here</a>.', Mage::helper('customer')->getEmailConfirmationUrl($customer->getEmail())));
$this->_redirectSuccess(Mage::getUrl('*/*/edit', array('_secure'=>true)));
return;
}
else {
$session->setCustomerAsLoggedIn($customer);
$url = $this->_welcomeCustomer($customer);
$this->_redirectSuccess($url);
return;
}
} else {
$session->setCustomerFormData($this->getRequest()->getPost());
if (is_array($errors)) {
foreach ($errors as $errorMessage) {
$session->addError($errorMessage);
}
}
else {
$session->addError($this->__('Invalid customer data'));
}
}
}
catch (Mage_Core_Exception $e) {
$session->setCustomerFormData($this->getRequest()->getPost());
if ($e->getCode() === Mage_Customer_Model_Customer::EXCEPTION_EMAIL_EXISTS) {
$url = Mage::getUrl('customer/account/forgotpassword');
$message = $this->__('There is already an account with this email address. If you are sure that it is your email address, <a href="%s">click here</a> to get your password and access your account.', $url);
$session->setEscapeMessages(false);
}
else {
$message = $e->getMessage();
}
$session->addError($message);
}
catch (Exception $e) {
$session->setCustomerFormData($this->getRequest()->getPost())
->addException($e, $this->__('Cannot save the customer.'));
}
}
$this->_redirectError(Mage::getUrl('customer/account/create', array('_secure' => true)));
}
Everything is posted apart from "howyouheard" input. The output of the log file that I created using Mage::log($customer, null, 'howyouheard_customer.log') function shows that I am getting my post data to this php file.
Also find below the code that I used for creating a new column in the existing database
<?php
$installer = $this;
$installer->startSetup();
$installer->getConnection()->addColumn(
$installer->getTable('customer_entity_grid'),
'howyouheard',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'length' => 255,
'nullable' => true,
'default' => NULL,
'comment' => 'How you heard about us'
)
);
$installer->endSetup();
?>
I also added the new version number in the XML file and ran the script, it created a new column in the existing database with null values as default. So basically the database is modified, front end has no issues, I am getting the post data but it doesn't insert into the database.
Can someone help me out with this issue?