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
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'];

Link to comment
Share on other sites

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
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.