Magento - Run Sql Query after save
<p>I have a custom module for magento which is working fine. What I want is that I have a sql update query which is run on form load, whereas I want it to run after save/submit.</p>

<p>The first of the form.php file is like this:</p>

<pre><code>&lt;?php
class Mywebsolutions_Autopriceupdate_Block_Adminhtml_Autopriceupdate_Edit_Tab_Form extends Mage_Adminhtml_Block_Widget_Form
{
        protected function _prepareForm()
        {
                $form = new Varien_Data_Form();
                $this-&gt;setForm($form);
                $fieldset = $form-&gt;addFieldset("autopriceupdate_form", array("legend"=&gt;Mage::helper("autopriceupdate")-&gt;__("Item information")));

                        $fieldset-&gt;addField("date", "date", array(
                        "label"     =&gt;Mage::helper ("autopriceupdate")-&gt;__("Date"),
                        "name" =&gt; "date",

                        'image' =&gt; $this-&gt;getSkinUrl('images/grid-cal.gif'),
                        'format' =&gt; Mage::app()-&gt;getLocale()-&gt;getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_MEDIUM)
        ));

                        $fieldset-&gt;addField("goldrate", "text", array(
                        "label" =&gt; Mage::helper("autopriceupdate")-&gt;__("GoldRate"),
                        "name" =&gt; "goldrate",
                        ));
                        /*          
                         $fieldset-&gt;addField('status', 'select', array(
                        'label'     =&gt; Mage::helper('autopriceupdate')-&gt;__('Status'),
                        'values'   =&gt; Mywebsolutions_Autopriceupdate_Block_Adminhtml_Autopriceupdate_Grid::getValueArray2(),
                        'name' =&gt; 'status',
                        ));*/

                if (Mage::getSingleton("adminhtml/session")-&gt;getAutopriceupdateData())
                {
                    $form-&gt;setValues(Mage::getSingleton("adminhtml/session")-&gt;getAutopriceupdateData());
                    Mage::getSingleton("adminhtml/session")-&gt;setAutopriceupdateData(null);
                } 
                elseif(Mage::registry("autopriceupdate_data")) {
                    $form-&gt;setValues(Mage::registry("autopriceupdate_data")-&gt;getData());
                }

                return parent::_prepareForm();
        }
}
</code></pre>

<p>The second part of from.php which I want to run after submit/save</p>

<pre><code>//database write adapter custom query by myweb solutions
$write = Mage::getSingleton('core/resource')-&gt;getConnection('core_write');

//update query for 22 carat making charges

$write-&gt;query("update catalog_product_entity_varchar cpev, (select value,entity_id from catalog_product_entity_varchar
where attribute_id=139) cpev1, eav_attribute eav, catalog_product_entity p, autopriceupdate a, catalog_product_flat_1 cpf
SET  cpev.value =round((((a.goldrate)/24)*22*cpf.weight)*cpev1.value/100,-1)
where

a.autopriceupdate_id=(
    SELECT max(autopriceupdate_id) FROM autopriceupdate
    )
and
eav.entity_type_id =4
AND eav.attribute_code = 'price'
AND p.entity_id = cpev.entity_id
AND p.attribute_set_id =10
AND cpev.attribute_id=140
and cpev1.entity_id = cpev.entity_id
AND cpf.entity_id =cpev.entity_id
AND cpf.metal_purity=4");

//myweb solutions using a time gap before execution of next query;
sleep(5);

//update query for 18 carat making charges

$write-&gt;query("update catalog_product_entity_varchar cpev, (select value,entity_id from catalog_product_entity_varchar
where attribute_id=139) cpev1, eav_attribute eav, catalog_product_entity p, autopriceupdate a, catalog_product_flat_1 cpf
SET  cpev.value =round((((a.goldrate)/24)*18*cpf.weight)*cpev1.value/100,-1)
where

a.autopriceupdate_id=(
    SELECT max(autopriceupdate_id) FROM autopriceupdate
    )
and
eav.entity_type_id =4
AND eav.attribute_code = 'price'
AND p.entity_id = cpev.entity_id
AND p.attribute_set_id =10
AND cpev.attribute_id=140
and cpev1.entity_id = cpev.entity_id
AND cpf.entity_id =cpev.entity_id
AND cpf.metal_purity=3");


//myweb solutions using a time gap before execution of next query;
sleep(5);

//update query for 22 carat price
$write-&gt;query("update catalog_product_entity_decimal val, eav_attribute eav, catalog_product_entity p, autopriceupdate a, 
catalog_product_flat_1 cpf, 
catalog_product_entity_varchar cpev
SET  val.value =round(((((a.goldrate)/24)*22*cpf.weight)+cpev.value),-1)
WHERE 
a.autopriceupdate_id=(
    SELECT max(autopriceupdate_id) FROM autopriceupdate
    )
and
eav.entity_type_id =4
AND eav.attribute_code = 'price'
AND p.entity_id = val.entity_id
AND p.entity_id = cpev.entity_id
AND cpev.attribute_id=140
AND p.attribute_set_id =10
AND val.attribute_id = eav.attribute_id
AND cpf.entity_id =val.entity_id
AND cpf.metal_purity=4"); 


//myweb solutions using a time gap before execution of next query;
sleep(5);

$write-&gt;query("update catalog_product_entity_decimal val, eav_attribute eav, catalog_product_entity p, autopriceupdate a, 
catalog_product_flat_1 cpf,
catalog_product_entity_varchar cpev
SET  val.value =round(((((a.goldrate)/24)*18*cpf.weight)+cpev.value),-1)
WHERE 
a.autopriceupdate_id=(
    SELECT max(autopriceupdate_id) FROM autopriceupdate
    )
and
eav.entity_type_id =4
AND eav.attribute_code = 'price'
AND p.entity_id = val.entity_id
AND p.entity_id = cpev.entity_id
AND cpev.attribute_id=140
AND p.attribute_set_id =10
AND val.attribute_id = eav.attribute_id
AND cpf.entity_id =val.entity_id
AND cpf.metal_purity=3"); 
</code></pre>

<p>How this can done in magento. </p>