Home » Magento16 August 2010

Magento: How to select, insert, update, and delete data?

Here, I will be showing how to select, insert/add, update/edit and delete data in the Magento way. It’s about implementing the CRUD (Create Read Update Delete) concept. :)

Suppose, I have a database table named ‘news‘ with the following fields:-

id : int, auto increment, primary key
title : varchar
content : text
status : 0 or 1

Suppose, I have a module named ‘mynews‘. Here follows the code to select, insert, update, and delete data from the ‘news‘ table.


$data contains array of data to be inserted. The key of the array should be the database table’s field name and the value should be the value to be inserted.


$item->getData() prints array of data from ‘news’ table.
$item->getTitle() prints the only the title field.
Similarly, to print content, we need to write $item->getContent().


$id is the database table row id to be updated.
$data contains array of data to be updated. The key of the array should be the database table’s field name and the value should be the value to be updated.


$id is the database table row id to be deleted.

In this way you can perform select, insert, update and delete in your custom module and in any magento code.

Hope this helps. Thanks.


Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Pingback: Tweets that mention Magento: How to select, insert, update, and delete data? | Mukesh Chapagain's Blog -- Topsy.com()

  • Gaurav

    Its nice.. I have got more help by this…

  • Darshana

    Nice post. It helped me. But I have problem when I update as I am not getting the id parameter of current record that is going to update. If I hardcode the id then it works fine. But I need it to pick the ID dynamically by the following request

    $id = $this->getRequest()->getParam(‘id’);

    Please guide me where this id is exactly passed to this function.

    Thanks in adavnce!

  • lb

    Hi, i need some help of finding , how magento when you add a new categoty insert it into the DB, meaning the code that does it. exp, in admin i added a new categorie in the field, what is the code in class that get the name from this field and then the code and class that insert it into the data base. i need it for a project and it drives me crazy for a while. thanks for any help.

  • lb

    any help plz?

  • @Darshana – For ‘id’, you can add a hidden field in your edit form.

  • @lb – In the following class, you will find create, move, update, delete, etc. function related to Magento Category.


    File path:- app\code\core\Mage\Catalog\Model\Category\Api.php

    You can extend this class to create category from your custom module.

    Hope this helps,

  • @lb – If the class Mage_Catalog_Model_Category_Api didn’t work fine then you can try this class as well Mage_Catalog_Model_Category_Api_V2

    File path:- app\code\core\Mage\Catalog\Model\Category\Api\V2.php


  • lb

    thx for your help, was very helpfull,

  • lb

    the two filles Mage_Catalog_Model_Category_Api and the other helps creat a new category in a new module, what i’m really looking for is , when you are in the backend/catalog/add new category and you enter a new category where is the code that takes what you have entered and insert it into you DB. and all the classes involved into it… somthing like adddata…. or set….

  • thanks for ur help . keep it up

  • rahil


    I am having problem to edit product from magento admin section it take around 5 min, to update product and throws error message :: 1205 Lock wait time out exceeds;

    please help if you have any solution i am using shared hosting.

  • Pingback: How To | Video, Torrent, Downloads | Poodlesnatcher()

  • Jai

    Hi, I wanna add image uploaded functionality on review Form and want to display with each review. Can you pls help me on this?

    Thanks in advance.

  • yogesh

    can you please teach me magento

  • Habibi_esf

    thank you,
    thank you,

    thank you,

  • Sandipgulati08

    i wana block the user after entering wrong password thrree times
    h we caN DO IT?

  • Imrank Pasha

    Hi! Mukesh i have a problem with saving the values of my custom fields i have created a custom module from which all these custom fields are showing on the registration page but when i save the page the values are not getting saved into the database can you find some solution for this

    *__(‘Employee Id’) ?>
                            <input type="text" name="employee_id" id="employee_id" value="htmlEscape($this->getFormData()->getEmployee_Id()) ?>” title=”__(‘Employee Id’) ?>” class=”input-text validate-employee_id required-entry”/>

  • Kathiresan

    Hi! Mukesh,

    its nice Article “Magento: How to select, insert, update, and delete data?”.
    keep it up 

  • Theavbunny

    Bunny, I just study PHP and sql, i have html form already and i have there are two file are delete.php, insert.php.  and i want create two button are delete and insert button. but i don’t know how to insert and delete php to form. help me please

  • Ivn

    Hi there,

    I have created a custom module and tried to create a custom database table. My module_Setup does show up in core_resource table but how do I go about saving/fetching data into my new table? Which files need editing, what classes to be created? Also my table doesn’t actually show up in database but the module entry is present in core_resource. Is that right? Or there is any other way of showing that table in the database with records? Please reply as I am trying to do this since ages now.

  • Elegant Rao

    i am following your blog and its been really great help. i just started developement of magento extension and i have been assigned with difficult of the task. i have to show a collection in a grid (using self join) my query works ok in sqlyog query browser but when i try to do it in magento (direct query) .. it doesn’t show up the grid.  need help over here. Magento is not easy to understand. :-(

  • Richard Hartman

    Thanks Mukesh,  you saved me alot of time again!

  • sorry for my “noob level”, but where i should run this code? in a new file or inside some magento file?

  • Surbhi Sheth

    Insertion Extension helps eCommerce websites to import products in bulk &
    integrate feeds into Magento store. With use of product insertion extension,
    they can easily import group products, Cross-sell, Upsell & Related
    products, CSV, PDF, and Image Files supported and more.

  • Abdullah

    Hi, Where did you specify the name of the database table in your code?

  • Ramakant Vishwakarma

    i have problem this code

    $data = array(‘title’=>’hello there’,’content’=>’how are you? i am fine over here.’,’status’=>1);
    $model = Mage::getModel(‘mynews/mynews’)->setData($data);
    try {
    $insertId = $model->save()->getId();
    echo “Data successfully inserted. Insert ID: “.$insertId;
    } catch (Exception $e){
    echo $e->getMessage(); }

    this code error setData

  • EEa

    Hi Mukesh,
    by using


    Mage::log(“After Delete: “.$model->getAttribute_Id());
    //Mage::log(“Data deleted successfully.”);

    * delete() funtion not wopking
    plz help me.

  • Naveen

    Fatal error: Call to a member function setData() on a non-object

  • Yogesh Yadav

    Hi Mukesh I have one problem $this->getRequest()->getParams(); show blank array on server how to solve it