Re: Delete multiple records from checkboxes

From: Date: Wed, 28 Jun 2000 02:22:15 +0000
Subject: Re: Delete multiple records from checkboxes
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-3492@lists.php.net to get a copy of this message
On 27 Jun, O.J. Alcaraz wrote: > I have a form where a user can delete records by checking the checkbox of > each desired item. I am having trouble performing a delete statement that > includes the WHERE IN clause. > > DELETE FROM MyTable > WHERE MyId IN (ID_1, ID_2, ID_X) > > Where ID_1, ID_2, ID_X are values from the form. How would I go about doing > this? I tried putting the $formvalue for ID_1, ID_2, ID_X to no avail. I'm > new to PHP so I'm sure this is something pretty easy. Thanks. > > O.J. Alcaraz > Abaris Technologies > Try another approach. In the form, name the checkboxes as an array. In the delete script, you can then use implode to convert the array passed in, into a comma separated list of items which you then feed to the query. Say your values from the form are in the array $formvalue[] you could do: $in_list = implode($formvalue, "'"); $query = "DELETE FROM MyTable WHERE MyId IN(" . $in_list . ")"; $result = mysql_db_query("database", $query); This assumes that MyId is an integer value; if it is a char type you'd have to do a little magic with $formvalue to add opening and closing ' to each element. Cheers -- David Robley | WEBMASTER & Mail List Admin RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/ AusEinet | http://auseinet.flinders.edu.au/ Flinders University, ADELAIDE, SOUTH AUSTRALIA

« previous php.general (#3492) next »