Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

How to use checkbox to delete mutiple rows from database

by lvb (Initiate)
on Nov 14, 2005 at 07:26 UTC ( [id://508231]=perlquestion: print w/replies, xml ) Need Help??

lvb has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How to use checkbox to delete mutiple rows from database

Replies are listed 'Best First'.
Re: How to use checkbox to delete mutiple rows from database
by bart (Canon) on Nov 14, 2005 at 08:53 UTC
    The only way to reliably delete specific rows from a database, is to have a unique property (id) for each row. Names are not necessarily unique. Worse, you may have double entries. If you try to delete a double of a row by user name using the name as identifier for the row, for example, that will result in both rows to be deleted. Not what you want.

    Deleting a row by ID is easy:

    DELETE FROM users WHERE id=?

    As for more than one row, databases start to digress. Some give you the option to use IN(1,2,3)... but personally, I'd just delete them one by one in a loop. It's not like that would take such a long time: typically a small query will execute in a few milliseconds.

    As you didn't say what kind of interface you're using, I can't go into much more details. In a HTML form you typically use the same name for every checkbox, and the id as the value. Then, for every checked checkbox, you'll get a parameter checkboxname=rowid. It's easy to loop through those.

Re: How to use checkbox to delete mutiple rows from database
by dorward (Curate) on Nov 14, 2005 at 07:58 UTC
    1. Get the values of the checked checkboxes (How you do that depends on what type of checkboxes they are. Tk? HTML via CGI? You didn't say.)
    2. Prepare an SQL statement that deletes one entry (determined by a bind variable). (Assuming you are using an SQL database, you didn't say.).
    3. Loop over the values you have, executing the SQL query (with the current value) each time.
Re: How to use checkbox to delete mutiple rows from database
by ghenry (Vicar) on Nov 14, 2005 at 08:22 UTC

    Or use one of the HTML::DBForm variants (as far as I can see HTML::DBForm doesn't do checkboxes, only radio buttons, tables or dropdown lists), which can provide this for you.

    HTH.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
Re: How to use checkbox to delete mutiple rows from database
by kulls (Hermit) on Nov 14, 2005 at 08:36 UTC

    Hi,
    you can get the list of users from the DB. Display the users and make sure that your checkbox value should identify the user_id ( may be whatever u can identify your user as unique ).When multiple users are selected,then in the CGI you can get it (user ids )in the array-context .If your db supports sub-query then, you just do,
    delete from tbl_name where id in (join(',',@usr_id_array));
    Oterwise, you just prepare a delete query (outside the loop )and execute the query in the for loop for each user id.
    This will delete all the users from the db.
    -kulls.

      And at least, using this technique, you open up your script to a lot of possible abuse. It's not too hard to post some additional SQL commands to your database this way.

      --
      b10m

      All code is usually tested, but rarely trusted.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://508231]
Approved by ysth
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-24 07:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found