Jump to content

Delete multiple entries


princeofpersia

Recommended Posts

Hi guys 

 

Im trying to delete multiple images from  a page with the code below, I have retirieved the images successfully but i can not delete the with checkbox,

 

wht it does is just refereshing the page and thats it, what im trying to do is to delete the image from the database but it wont.

 

can u help me please?

 

<?php
session_start();

include ("../../global.php");
//welcome messaage
$username=$_SESSION['username'];
echo "$username";

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
while($row = mysql_fetch_assoc($query))
{
$user_id = $row['id'];
}

$ref=$_GET['reference'];


$images=mysql_query("SELECT * FROM img WHERE refimage='$ref'");

while($row = mysql_fetch_array($images))
{
$image=$row['image'];
$thumb=$row['thumb'];





?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="" method="post" action=''>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo "<a href='$image' rel='lightbox[roadtrip]'><img src= '$thumb' width='60' height='40' alt='$title'>";?></td>
<?
}
?>


<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>


<?php

// Check if delete button active, start this
$delete = $_REQUEST['delete'];
if( $delete != '' ){
$checkbox = $_REQUEST['checkbox'];
$count = count($_REQUEST['checkbox']);
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$del = mysql_query("DELETE FROM img WHERE id = '$del_id'");

}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/223389-delete-multiple-entries/
Share on other sites

a couple tips: if there is only one record, you don't need a loop here. and you should always check mysql_error() after mysql_query:

 

$query=mysql_query("SELECT id FROM users WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_assoc($query);
$user_id = $row['id'];

so there is more than one id for each username? then this doesn't make sense, because you loop over all of them changing the value each time but only ending up with the very last one.

 

$query=mysql_query("SELECT id FROM users WHERE username='$username'");
while($row = mysql_fetch_assoc($query))
{
$user_id = $row['id']; // Set and set and set over and over
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.