...

View Full Version : checkboxes, arrays and delete statement



jeskel
03-24-2004, 10:55 AM
Hi :)

here is the basic code used to display data (texts titles). I'd like to allow multiple deletes thank to checkboxes. I should get all the pks in an array and delete them all. I am kinda stuck with the array part...



$sql = @mysql_query("SELECT pk, title, FROM texts ORDER BY pk DESC");
if (!$sql) {
die('<p>Error performing query: ' . mysql_error() .
'</p>');
}

while ( $row = mysql_fetch_array($sql) ) {
?>
<p>
<input type="checkbox" value="<?=$row['pk']?>" name="pk">
<a href="display.php?text=<?=$row['pk']?>"><?=$row['title']?></a>
</p>


any help out there ;)?

:)

raf
03-24-2004, 03:49 PM
why do you need the array ?


if you post the form (with a few boxes chackt) and you do a

$_POST['pk']

then you should get them printed as a commadelimited list.

So if you use


"delete from table where pk In (". $_POST['pk'] . ")"

then that shoudl do the trick.
But i alsays add the pk to the name and then loop through the $_POST to build the collection.

jeskel
03-24-2004, 04:41 PM
Originally posted by raf

But i alsays add the pk to the name and then loop through the $_POST to build the collection.
an example to post? ;)

ConfusedOfLife
03-24-2004, 05:14 PM
I think you should change your form to this:



while ( $row = mysql_fetch_array($sql) ) {
?>
<p>
<input type="checkbox" value="<?=$row['pk']?>" name="pk[]">
<a href="display.php?text=<?=$row['pk']?>"><?=$row['title']?></a>
</p>


And then you can use:


$commaDelimited = implode( ", ", $_POST["pk"] );
$res = mysql_query("delete from table where pk In ( $commaDelimited )");

jeskel
03-24-2004, 05:15 PM
*cross posts COL :) let me give it a try.

For some reasons, it only deletes one item at a time...?

form:


<form action="editDeleteUserCP.php" method="POST">
<?php
while ( $row = mysql_fetch_array($result) ) {
?>
<p>
<input type="checkbox" value="<?=$row['pk']?>" name="textID">
<a href="editUserCP.php?text=<?=$row['pk']?>"><?=$row['title']?></a>
</p>
<?php
}
?>
<input type="submit" value="delete" />
</form>

deletion script:


$textID = $_POST['textID'];

$sql = @mysql_query("DELETE FROM links WHERE pk IN (". $textID . ")");
if (!$sql) {
die('<p>Error performing query: ' . mysql_error() .
'</p>');
}

header("Location:indexUserCP.php");
exit;

jeskel
03-24-2004, 05:24 PM
ok it works great :)

ConfusedOfLife
03-24-2004, 05:28 PM
Originally posted by jeskel
*cross posts COL :) let me give it a try.

For some reasons, it only deletes one item at a time...?

form:


<form action="editDeleteUserCP.php" method="POST">
<?php
while ( $row = mysql_fetch_array($result) ) {
?>
<p>
<input type="checkbox" value="<?=$row['pk']?>" name="textID">
<a href="editUserCP.php?text=<?=$row['pk']?>"><?=$row['title']?></a>
</p>
<?php
}
?>
<input type="submit" value="delete" />
</form>

deletion script:


$textID = $_POST['textID'];

$sql = @mysql_query("DELETE FROM links WHERE pk IN (". $textID . ")");
if (!$sql) {
die('<p>Error performing query: ' . mysql_error() .
'</p>');
}

header("Location:indexUserCP.php");
exit;


Did you notice my post?!

jeskel
03-24-2004, 07:59 PM
Originally posted by jeskel
ok it works great :)
was about your post ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum