CodeIgniter Forums
Delete multiple rows from database table - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Delete multiple rows from database table (/thread-76354.html)



Delete multiple rows from database table - eleumas - 05-05-2020

PHP Code:
  function delete()
  {
    foreach ($this->request->getPost('checkbox') as $id)
    {
      $path_thumb '../media/images/uploads/'.$id.'/thumb';
      if (is_dir($path_thumb))
      {
        delete_files($path_thumb); 
        rmdir($path_thumb);
      }

      $path '../media/images/uploads/'.$id;
      if (is_dir($path))
      {
        delete_files($path); 
        rmdir($path);
      }

      $articlesModel = new ArticlesModel();
      $imagesModel = new ImagesModel();

      $deleteImages $imagesModel->where('id_article'$id)->delete();
      $deleteArticles $articlesModel->where('id'$id)->delete();

      if($deleteImages && $deleteArticles)
      {
        return redirect()->to(base_url('articles'))->with('success''Gli articoli selezionati sono stati eliminati!');
      }
      else
      {
        return redirect()->to(base_url('articles'))->with('danger''Errore! Impossibile eliminare i dati nel database.');
      }
    

I would like delete multiple rows from my table. I have an array of checkbox. How can i do? Thanks.


RE: Delete multiple rows from database table - jreklund - 05-05-2020

You can use whereIn() to target multiple IDs.


RE: Delete multiple rows from database table - eleumas - 05-05-2020

If i use this snippet:
PHP Code:
$deleteImages $imagesModel->whereIn('id_article'$id)->delete();
$deleteArticles $articlesModel->whereIn('id'$id)->delete(); 

I have an error:
Code:
CodeIgniter\Database\Exceptions\DatabaseException #8
Deletes are not allowed unless they contain a "where" or "like" clause.

Please, can you write the correct query for delete multiple rows? Thanks for help me.


RE: Delete multiple rows from database table - jreklund - 05-05-2020

I think you are required to put in an array. Right now you are just entering one id.

This need to be run outside your foreach, as you need to populate $ids array with all $id elements.
PHP Code:
$ids = [12345]; // Id's to delete.
$deleteImages $imagesModel->whereIn('id_article'$ids)->delete();
$deleteArticles $articlesModel->whereIn('id'$ids)->delete(); 



RE: Delete multiple rows from database table with checkbox - eleumas - 05-06-2020

(05-05-2020, 11:35 AM)jreklund Wrote: I think you are required to put in an array. Right now you are just entering one id.

This need to be run outside your foreach, as you need to populate $ids array with all $id elements.
PHP Code:
$ids = [12345]; // Id's to delete.
$deleteImages $imagesModel->whereIn('id_article'$ids)->delete();
$deleteArticles $articlesModel->whereIn('id'$ids)->delete(); 

This is the solution for delete multiple rows:

PHP Code:
$id $this->request->getPost('checkbox');
        
$ids = [];

        for($count 0$count count($id); $count++)
        {
          $ids[] = $id[$count];
        }

        $articlesModel = new ArticlesModel();
        $imagesModel = new ImagesModel();
        $deleteImages $imagesModel->whereIn('id_article'$ids)->delete();
        $deleteArticles $articlesModel->whereIn('id'$ids)->delete();