Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1024905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:50:51+00:00 2026-05-16T11:50:51+00:00

I am using the Magento’s functionality to insert & update queries. My requirement is

  • 0

I am using the Magento’s functionality to insert & update queries. My requirement is that I want to take care of SQL Injection, when doing these types of queries. But I’m unable to find how Magento does this. I’m providing one start sample. Please provide me with one complete example.

<?php
$write = Mage::getSingleton("core/resource")->getConnection("core_write");
$sql = "INSERT INTO Mage_Example (Name, Email, Company, Description, Status, Date)
    VALUES ('$name', '$email', '$company', '$desc', '0', NOW())";
?>

Now I want to change the above query to prevent the possible SQL Injection. I don’t want to use the default “mysql_real_escape_string()” built-in function of PHP. Can anybody please provide me with one useful solution, using the “$write” DB Handler.

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T11:50:51+00:00Added an answer on May 16, 2026 at 11:50 am

    Okay, researched this one a little bit. If you can get an instance of a DB_Adapter (which I believe that resource call will return), this shouldn’t be too tough. Deep down inside, Magento is based on Zend Framework, and the DB adapter specifically is descended from Zend_Db_Adapter, so you can use those methods for free. See the link before for more examples, but here’s the syntax provided in the docs, which should escape your input automagically:

    $write = Mage::getSingleton("core/resource")->getConnection("core_write");
    
    // Concatenated with . for readability
    $query = "insert into mage_example "
           . "(name, email, company, description, status, date) values "
           . "(:name, :email, :company, :desc, 0, NOW())";
    
    $binds = array(
        'name'    => "name' or 1=1",
        'email'   => "email",
        'company' => "company",
        'desc'    => "desc",
    );
    $write->query($query, $binds);
    

    Again, see the docs for more information.


    UPDATE:

    I’ve changed the example above. The object that you get back with your core_write request is a PDO object that exposes a query method (see above) that will let you used parameterized queries. This is BY FAR a better approach than attempting to use something like mysql_real_escape_string for data sanitization, and I’ve tested the above code for correctness. Note that, in contrast to most MySQL parameterized queries, the binding is done with :labels, and also that you need no quotes for your vars.

    In response to your other point, and as noted below, the “right” way to do it in Magento is not to use direct queries at all. The Magento object models are well development and meant to abstract this kind of implementation detail away from you, because you shouldn’t need to concern yourself with it. To do it “correctly”, create a new database-based model and save the headache.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to translate a frontend Magento store using only one csv file. So
I just discovered on my site using magento 1.3.2.2 that on a bundled product,
I`m using Magento 1.3 and I want to test the new 1.4, but is
i am using Magento 1.5.1.0. I want to remove the message textarea as an
Using Magento i want to create website where user can upload their products to
I'm using magento 1.5. I have a requirement to install a third-party ecommerce package.
I'm using Magento 1.4 and I want to remove the Checkout and My Cart
I'm using Magento 1.5.0.1 and I want to move my advanced search form to
i'm using magento version 1.3.2.4 I want to change a product shipping as free
I'm using Magento, and I want to rewrite Mage_Catalog_Block_Product_Price, but a community codePool module

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.