princeofpersia Posted January 4, 2011 Share Posted January 4, 2011 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'"); } } ?> Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 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']; Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 4, 2011 Author Share Posted January 4, 2011 its more than 1 record listed Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 4, 2011 Author Share Posted January 4, 2011 the problem is deleting the check images Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 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 } Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 4, 2011 Author Share Posted January 4, 2011 no there is one, sorry i though u r talking about images any suggesstion about the delete functionality? Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 another tip: if there is anything inserting, updating or deleting records, perform that logic at the top of the script before any HTML is output. and if the form is POST'ed use $_POST. if it is GET'ed use $_GET. But try to avoid $_REQUEST Quote Link to comment Share on other sites More sharing options...
princeofpersia Posted January 4, 2011 Author Share Posted January 4, 2011 thanks but what im really keen on is delete function Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.