daebat Posted January 3, 2011 Share Posted January 3, 2011 I'm working on an old cms I built a few years ago and for some reason my Delete page isn't working. It has to be something simple I'm overlooking. The database connection is made just above this code and is pulling the thumb and title listed by id. When I push delete I'm getting the cmd=delete&id=2 passed on so something is up with my last if statement (I think). Here is the code: if(!isset($cmd)) { //display all the news $result = mysql_query("select * from verizon order by id"); //run the while loop that grabs all the news scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the enws $title=$r["title"];//take out the title $id=$r["id"];//take out the id $thumb=$r["thumb"]; echo "<div id='delete'> <table> <tr> <td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete&id=$id'> Delete</a></td> </tr> <td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td> </table> </div><br />"; } } if($cmd=="delete") { $sql = "DELETE FROM verizon WHERE id='$id'"; $result = mysql_query($sql); echo "<h1 style='text-align:center;'>deleted!</h1>"; } } ?> Quote Link to comment Share on other sites More sharing options...
daebat Posted January 3, 2011 Author Share Posted January 3, 2011 Ok, I got it one step forward. By setting $cmd=$_GET['cmd']; I got it to run through the if statement but now it's not performing the actual query to delete the item from the database. I tried setting up $id=$_GET['id']; but it didn't work. Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 3, 2011 Share Posted January 3, 2011 thats because you have the delete statement inside the brackets that say if cmd is not set move it outside the brackets Quote Link to comment Share on other sites More sharing options...
daebat Posted January 3, 2011 Author Share Posted January 3, 2011 nah I just didn't show the portion just before it. Here is the full script with my new stuff added: if($session->logged_in){ $cmd=$_GET['cmd']; //If cmd has not been initialized if(!isset($cmd)) { //display all the news $result = mysql_query("select * from verizon order by id"); //run the while loop that grabs all the news scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the enws $title=$r["title"];//take out the title $id=$r["id"];//take out the id $thumb=$r["thumb"]; echo "<div id='delete'> <table> <tr> <td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete&id=$id'> Delete</a></td> </tr> <td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td> </table> </div><br />"; } } if($cmd=="delete") { $sql = "DELETE * FROM verizon WHERE id=$id"; $result = mysql_query($sql); echo "<h1 style='text-align:center;'>deleted!</h1>"; } } else{ echo "<div style='text-align:center'>[<a href='../main.php'>Please Login</a>] </div>"; } The script is now returning the echo of "Deleted!" but the query isn't running because it never deletes. Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 3, 2011 Share Posted January 3, 2011 you do need to have this aswell $id=$_GET['id']; after you do that echo the id to make sure you are getting what you are expecting Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 3, 2011 Share Posted January 3, 2011 i have re written it a bit for you try this if($session->logged_in){ $cmd=$_GET['cmd']; $delID=$_GET['id']; if(isset($cmd)&&$cmd=='delete') { $sql = "DELETE FROM verizon WHERE id='$delID'"; $result = mysql_query($sql); echo "<h1 style='text-align:center;'>deleted!</h1>"; } //If cmd has not been initialized if(!isset($cmd)) { //display all the news $result = mysql_query("select * from verizon order by id"); //run the while loop that grabs all the news scripts while($r=mysql_fetch_array($result)) { //grab the title and the ID of the enws $title=$r["title"];//take out the title $id=$r["id"];//take out the id $thumb=$r["thumb"]; echo "<div id='delete'> <table> <tr> <td width='350px'><strong>$title</strong></td><td><a href='verizondelete.php?cmd=delete id=$id'> Delete</a></td> </tr> <tr> <td colspan='2' align='center'><img src='../upload/verizon/$thumb'></td> </tr> </table> </div><br />"; } } } else { echo "<div style='text-align:center'>[<a href='../main.php'>Please Login</a>] </div>"; } Quote Link to comment Share on other sites More sharing options...
daebat Posted January 3, 2011 Author Share Posted January 3, 2011 I swapped out the code and it didn't work :\ not sure what's going on here? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 3, 2011 Share Posted January 3, 2011 The code that was posted somehow ended up with a in the links instead of a & and won't work. You would need to fix the HTML in the link. Quote Link to comment Share on other sites More sharing options...
daebat Posted January 3, 2011 Author Share Posted January 3, 2011 Still no dice... I'm getting the confirmation echo of 'deleted!' but something must be wrong with this: if(isset($cmd)&&$cmd=='delete') { $sql = "DELETE FROM verizon WHERE id='$delID'"; /* <------------- */ $result = mysql_query($sql); /* <------------ */ echo "<h1 style='text-align:center;'>deleted!</h1>"; } Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 3, 2011 Share Posted January 3, 2011 Post the output of echo $sql; and the output of echo '<pre>'; print_r($_GET); echo '</pre>'; Quote Link to comment Share on other sites More sharing options...
daebat Posted January 3, 2011 Author Share Posted January 3, 2011 My mistake. Everything worked! Thanks 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.