Re: Deleting multiple names from the database at once

From: Date: Fri, 01 Sep 2000 17:50:02 +0000
Subject: Re: Deleting multiple names from the database at once
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-14847@lists.php.net to get a copy of this message
At 7:01 PM +0200 9/1/00, Siim Einfeldt wrote:
Hi all Last email I received from some mailing-list, started "Hi, My name is John and I have a problem..". I thought I won`t start like this, but I still need some help. Anyway, I have a list of names, e.g ->John ->Mary ->Someone else I get these names from the database using while this, echo this method. e.g: $getinimesed = mysql_query("SELECT * FROM inimesed");
	        echo "<FORM METHOD=POST
ACTION='adminpage.php3?adminame=$adminame&adminpass=$adminpass&id=id'>";
	      	while ($see = mysql_fetch_array($getinimesed)){
	      		$nimi = $see["name"];
	      		$code = $see["isikukood"]
	        echo "<INPUT TYPE='checkbox' name='$code'><a
href='javascript:openit()'>$name</a><BR>";
	     	}
echo "<INPUT TYPE='Submit' VALUE='DELETE'>
	     	</FORM>";
Right now it puts the checkbox in front of all names and below is the sumbit button. How can I delete all the names, which are checked (checkboxes), from the database? Logically taken, it should be quite easy, but I thought about it and havent`t managed to solve this thing myself. So any help is appreciated. Thank you in advance, Siim Einfeldt
I would do something like this: ... while ($see = mysql_fetch_array($getinimesed)) { $nimi = $see["name"]; $code = $see["isikukood"] echo "<INPUT TYPE='checkbox' name='delete_list[]' value='$code'><a href='javascript:openit()'>$name</a><BR>"; } ... Note that I put the code in the VALUE attribute, and indicated that we want to return an array of names by using the 'delete_list[]' form (see: http://www.php.net/manual/language.variables.external.php ; in particular, check the first of the user-submitted comments). Then, in the program that processes the deletions, you could do (untested!): ... $Query = 'DELETE FROM inimesed WHERE code IN ('. implode(',' $delete_list).')'; $result = mysql_query($Query); ... This assumes that 'code' is numeric; if not, you'll have to quote each of the values. You could do that via: reset($delete_list); for ($i=0; $i<count($delete_list); $i++) { $delete_list[$i] = "'".$delete_list[$i]."'"; } or via the array_walk() function; see http://www.php.net/manual/function.array-walk.php For more info on Mysql's delete command, see http://www.mysql.com/documentation/mysql/commented/manual.php?section =DELETE -steve +--- "They've got a cherry pie there, that'll kill ya" ------------------+
| Steve Edberg                           University of California, Davis |
| sbedberg@ucdavis.edu                               Computer Consultant |
| http://aesric.ucdavis.edu/                  http://pgfsun.ucdavis.edu/ |
+-------------------------------------- FBI Special Agent Dale Cooper ---+

« previous php.general (#14847) next »