phatuis Posted October 22, 2009 Share Posted October 22, 2009 What I am trying to do, is say I have 1 piece of data with id 1, one with id 2, and one with 3. Using this code, I can successfully delete the selected data by id, but also what I want to do is for every id above the one deleted, I want to make them go down by 1, like the one with id of 2 would go to 1, and the one with id of 3 would go to 2, the code below is what I have tried, and been unsuccessful This is my code so far: $sqldelete = "DELETE from `banners` WHERE id='".$deleteid."'"; $resdelete = mysql_query($sqldelete) or die(mysql_error()); $upsql = "SELECT * FROM `banners` WHERE id > '".$deleteid."'"; $upres = mysql_query($upsql) or die(mysql_error()); $array[] = $upres; foreach($array AS $idnum) { $idstart = $deleteid + $idnum; $idupdate = $idstart - 1; $sqlupdate = "UPDATE `banners` SET id='".$idupdate."' WHERE id='".$idstart."'"; $sqlres = mysql_query($sqlupdate); } echo "You have successfully deleted the item with an ID of ".$deleteid."!"; Quote Link to comment Share on other sites More sharing options...
Gayner Posted October 22, 2009 Share Posted October 22, 2009 What is the deleteid variable, u don't have it set .. ? Quote Link to comment Share on other sites More sharing options...
phatuis Posted October 22, 2009 Author Share Posted October 22, 2009 When I click delete next to the data on the main page, the deleteid is found using GET, so like if the extension is admin.php?delete=1 , then the id being deleted is 1 Quote Link to comment Share on other sites More sharing options...
Gayner Posted October 22, 2009 Share Posted October 22, 2009 When I click delete next to the data on the main page, the deleteid is found using GET, so like if the extension is admin.php?delete=1 , then the id being deleted is 1 So your not posting all ur code? lol bro we need to look at it all, sorry to be rude but just it helps alot to see what's going on everywhere u know man? If it getting $_GET correctly then it should work, post all. let's me see. echo that $deleteid variable to see what's it's calling too to make sure. Quote Link to comment Share on other sites More sharing options...
phatuis Posted October 22, 2009 Author Share Posted October 22, 2009 All Code: <?php include("global.php"); connect(); $sql = "SELECT * FROM `banners` WHERE admin='1' AND username='".$session_username."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 1) { $sql2 = "SELECT * FROM `banners`"; $res2 = mysql_query($sql2) or die(mysql_error()); if(isset($_GET['delete'])) { $deleteid = $_GET['delete']; if(!isset($_GET['sure'])) { echo "<center>Are you sure you want to delete this data?<br><br>"; echo "<a href='admin.php?delete=".$deleteid."&sure=1'>Yes, I am sure</a<br>"; echo "<a href='admin.php'>No, I am not.</a></center>"; }else { $sqldelete = "DELETE from `banners` WHERE id='".$deleteid."'"; $resdelete = mysql_query($sqldelete) or die(mysql_error()); $upsql = "SELECT * FROM `banners` WHERE id > '".$deleteid."'"; $upres = mysql_query($upsql) or die(mysql_error()); $array[] = $upres; foreach($array AS $idnum) { $idstart = $deleteid + $idnum; $idupdate = $idstart - 1; $sqlupdate = "UPDATE `banners` SET id='".$idupdate."' WHERE id='".$idstart."'"; $sqlres = mysql_query($sqlupdate); } echo "You have successfully deleted the item with an ID of ".$deleteid."!"; } } else { while($assoc = mysql_fetch_assoc($res2)) { $id = $assoc['id']; $username = $assoc['username']; $sitename = $assoc['sitename']; $bannerurl = $assoc['bannerurl']; $url = $assoc['url']; echo "<center><table border='1'>"; echo "<tr><td>".$username."</td><td>".$sitename."</td><td>".$bannerurl."</td><td>".$url."</td><td><a href='admin.php?delete=".$id."'>Delete</a></td></tr>"; echo "</table></center>"; } } }else { die("You do not have permission to access this area"); } ?> Quote Link to comment Share on other sites More sharing options...
Gayner Posted October 22, 2009 Share Posted October 22, 2009 eDIT: NVM.. Hmm I'll get back to u in a bit Quote Link to comment Share on other sites More sharing options...
phatuis Posted October 22, 2009 Author Share Posted October 22, 2009 Wouldnt it be set in the foreach loop, $array AS $idnum? Quote Link to comment Share on other sites More sharing options...
Gayner Posted October 22, 2009 Share Posted October 22, 2009 Wouldnt it be set in the foreach loop, $array AS $idnum? Yea I edited post i was skimming thru it to fast I am kinda lost on what u said it's not doing? ur saying if &delete=2 then use id 1 to delete? I am confused Quote Link to comment Share on other sites More sharing options...
phatuis Posted October 22, 2009 Author Share Posted October 22, 2009 I'll break it down $idstart is what the id starts as, like for the first foreach loop the id will be $deleteid + 1, because it is in the first loop, then on the second it is $deleteid + 2. $idupdate is simply $idstart - 1, which then lowers each id by one after deletion. Quote Link to comment Share on other sites More sharing options...
phatuis Posted October 22, 2009 Author Share Posted October 22, 2009 If this cant be fixed, I have an idea, maybe a while loop with say a variable called idnum, which for each loop goes up 1, then I set idstart as deleteid + idnum, so then it loops upwards until the last id? Quote Link to comment Share on other sites More sharing options...
cags Posted October 22, 2009 Share Posted October 22, 2009 I have only briefly browsed the replies since the opening of the thread as I don't like the direction they were going in. Firstly why do you wish the id's to remain perfectly sequential? id's are generally of no real relevance other than for identifying the field. Secondly you shouldn't need to do any looping to achieve your objective, something like the following should suffice... $sql = "DELETE FROM table WHERE id=$deleteid"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $sql = "UPDATE table SET id=id-1 WHERE id > $deleteid"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); Quote Link to comment Share on other sites More sharing options...
phatuis Posted October 22, 2009 Author Share Posted October 22, 2009 I worked that one out about 10 minutes after I posted my last post, but thanks anyway guys. 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.