Magento Enterprise – How to Insert into a Magento Database Table

magento-enterpriseMySQL

Magento Enterprise 1.14.0.1

I have a plugin that I am altering the IndexController file for so that it will insert data collected from a user into a database table I created.

So basically I altered the Magento database to have new table which I am calling
sample_requests. My issue is that when the IndexController is fired nothing is being inserted into the database. All database columns are Varchar(255) with the exception of the request_date column which is a datetime column type.

below is my code which I have in the IndexController.php file

    $personal_name = 'test';
    $customer_email = 'boo';
    //Open Database Conenction
      $resource = Mage::getSingleton('core/resource');
      $writeConnection = $resource->getConnection('core_write');

      //The Query
        $query = "insert into magento.sample_requests
                       (first_name, last_name, email_addr, address, city, state, zip, country, customeri_id, request_date) 
                  values($personal_name, NULL, $customer_email, sdf, sdf, sdf, sdf, sdf, 0, NOW())";


        $writeConnection->query($query);

Best Answer

value must be in ''

 $query = "insert into magento.sample_requests
                       (first_name, last_name, email_addr, address, city, state, zip, country, customeri_id, `request_date`) 
                  values($personal_name,' NULL',$customer_email,'sdf', 'sdf', 'sdf', 'sdf', 'sdf', '0', NOW())";
Related Topic