Magento 1.9 SQL – How to Delete Rows from Database

databasemagento-1.9modulesql

MY custom database, is getting data from a form, and is inserted when the consumer reach a certain page. I can't make it any other way (because of reasons)

the page is an easy access page, and people (and bots) can reach it easy.
It result of a lot of empty rows in my table, and I want to write a code that delete all empty row, each time they reach the that page.

When I'm inserting the data I use below code:

$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "INSERT INTO mage_emailorder (emailorder_id, order_date, product_name, location, date_for, name_title, email, company, adress, zipcode, city, phone, ean, comment, order_done, number_of_entries)
VALUES (NULL, '$newdate', '$conferance_name', '$hotel', '$datefor', '$ogtitel', '$email', '$virksomhed', '$adresse', '$postnr', '$by', '$telefon', '$ean', '$kommentarer', '0', '$entries')";



try {
    $conn = Mage::getSingleton('core/resource')->getConnection('core_write');
    $conn->query($sql);
} catch (Exception $e){
    echo $e->getMessage();
}

the deleting code

$sql2 = "DELETE FROM mage_emailorder WHERE product_name IS NULL;"



try {
    $conn = Mage::getSingleton('core/resource')->getConnection('core_write');
    $conn->query($sql2);
} catch (Exception $e){
    echo $e->getMessage();
}
?>

My code for deleting is giving me error in the try{ }catch(){}, can some one help me with why?

Best Answer

I don't have enough rep here yet so, I can't add a comment. But from the error you got:

syntax error, unexpected 'try' (T_TRY) in /var/www/ifp.dk/public_html/app/design/frontend/ultimo/default/template/kolorcat‌​/success.phtml on line 44 

You have this before the try / catch block:

$sql2 = "DELETE FROM mage_emailorder WHERE product_name IS NULL;"

when it should actually be:

$sql2 = "DELETE FROM mage_emailorder WHERE product_name IS NULL";

Also, isn't there a way to prevent insert if values are not set ? I think you should give that a thought. It'd save you the overhead cost of this delete query :)

Related Topic