Lodder
Lodder

Reputation: 19743

Delete specific amount of rows from SQL database

I am trying to create a mass delete button. I am trying to have an input field, where the admin can specify the amount of rows he/she wants to delete.

So far, I am using the following code to try and delete the amount specified in the input field, but nothing happens and there are no errors in the error log:

helper.php

function deleteall($all) {
    $db = JFactory::getDBO();
    $query = $db->getQuery(true);
    $query->delete()
    ->from('#__shoutbox');
    $db->setQuery($query, 0, $all);
    $db->query();
}

mod_shoutbox.php

if(isset($post['deleteall'])) {
    $all = $post['all'];
    modShoutboxHelper::deleteall($all);
}

default.php

<form method="post" name="deleteall">
    <input name="all" type="text" value="" />
    <input name="deleteall" type="submit" value="mass delete" />
</form> 

I believe the SQL query is fine and that the problem is to do with the HTML in the default.php but not 100% sure. Can someone please let me know where I am going wrong?

Update:

$post has already been defined as I am using Joomla coding standards.

Upvotes: 1

Views: 240

Answers (3)

Techie
Techie

Reputation: 45134

I guess what you are trying to do is not accomplishable due to below reason. How can you tell the db which rows to be deleted ? You obviously should have a filter. EG : DELETE FROM table_name WHERE ID BETWEEN 1 AND 2999. Here rows only which have a ID between 1 & 2999 are deleted.

Else what you want is LIMIT. EG : DELETE FROM table_name WHERE ID BETWEEN 1 AND 2999 LIMIT 1000. LIMIT can limit the rows to be deleted.

DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name
    [PARTITION (partition_name,...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

Here you don't have any syntax errors, where you have gone wrong is the logic. Hope that explains everything. Let me know if you have any issue.

Upvotes: 1

Sorin Trimbitas
Sorin Trimbitas

Reputation: 1497

The problem is that the method deleteall is not receiving it's $all parameter (it received nothing so 0 is used instead by the query class).

Upvotes: 0

Colin Schoen
Colin Schoen

Reputation: 2598

Your post handler is incorrect. It should be $_POST['deleteall']

Upvotes: 0

Related Questions