Forum Moderators: coopster

Message Too Old, No Replies

Delet several rows selected with multiple check boxes

How to remove multiple records selecting multiple checkboxes

         

kernel undead

10:11 pm on Nov 17, 2005 (gmt 0)

10+ Year Member



I have a page that shows me a lot of records and i am deleting them one by one. how can i add a checkbox and select 4 or all of them and delete them. can you help me?

coopster

5:34 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, kernel undead.

The PHP manual has a page regarding PHP and HTML [php.net] that describes how to do what you ask. Well, not entirely. It describes how to setup form input items so that they can be looped through on the server-side by PHP in an array variable.

Here is a somewhat relative script that has a code snippet. You can modify the <select> input item to checkbox and play around a bit to get a feel for how it works in PHP.

Anyango

5:37 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello kernel

Welcome to Webmasterworld. That's pretty easy.

let's say each of your records has a unique id right or primary key, lets say that is

"recordid"

when you display those records , you can add a checkbox for each record and assign value=recordid of that record to its checkbox. now when the form is submitted to your action script, you can simply check for all the values of that checkbox and store thenm in a string in comma seperated format right, now you can simply run a query like

DELETE FROM RECORDS WHERE RECORDID IN (1,2,3,4,5)

where 1,2,3,4,5 in the above query came from the recordid s of the records which were "checked".

<edit>
Ah sorry, i didnt know coopster was posting meanwhile, otherwise i would listen to his words more then saying mine ;)
</edit>

coopster

5:47 am on Nov 18, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



no, no, that's fine, Anyango -- that's the best part of a community forum, we can all throw in to help ;-)

I did, however, forget that link with a sample snippet:
[webmasterworld.com...]

kernel undead

5:35 pm on Nov 18, 2005 (gmt 0)

10+ Year Member



thanks for your heplp! i did it like this:

I have this form:

<td><form id="form1" name="form1" method="post" action="remover_email.php">
<table width="40%" border="0" align="center" cellpadding="1" cellspacing="1">
<tr class="menu">
<td width="8%"><div align="center"></div></td>
<td width="92%">E-mail</td>
</tr>
<?php
$p = 0;
do {?>
<tr class="camposinsertos">
<td><div align="center">
<input name="email<? echo $p;?>" type="checkbox"
value="<? echo "$row_remover[id]";?>" />
</div></td>
<td><?php echo $row_remover['mail'];?></td>
</tr>


<?php $p++;} while ($row_remover = mysql_fetch_assoc($remover));?>
<tr>
<td><input type="hidden" name="cantidad" value="<? echo $p;?>" />
</td>
</tr>
<tr><td><input type="submit" value="Remover" /></td></tr>

</table>
<table width="40%" border="0" align="center" cellpadding="1" cellspacing="1">
<tr>
<td>&nbsp;</td>
</tr>
<tr class="etiquetas">
<td>
<input type="checkbox" name="marcatodos" onClick="todos()">Marcar todos ¦

<input type="checkbox" name="desmarcatodos" onClick="ninguno()">Desmarcar todos</td>
</tr>
</table>
<p>&nbsp;</p>
</form>

(I want to apologize for spanish words. from guatemala.)

when i submit the form:

<?php require_once('Connections/mercadeo.php');?>

<?
mysql_select_db($database_mercadeo, $mercadeo);
for ($i = 0; $i<=$_POST[cantidad]; $i++){
$nombre = "email".$i;
$sql = "DELETE FROM remover WHERE id = '$_POST[$nombre]'";
$result = mysql_query($sql, $mercadeo);
}

echo "<script language=\"Javascript\">\r\n";
echo "document.location.href='remover2.php';\r\n";
echo "</script>\r\n";

?>

dreamcatcher

6:26 pm on Nov 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working ok. I use the method explained by Anyango. Assign the checkboxes to an array, them implode the array and use the IN clause. No need for loops.

dc