Simple delete problems.

Thread Solved

Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Simple delete problems.

 
0
  #1
Feb 21st, 2007
Hi all, I'm just learning how to delete records from a test table but I'm having some problems with it. I'm not sure exactly but it looks like it is deleting the last row from the table no matter which record is clicked on. It also appears that it is deleting all records after the record clicked on but excluding that one. If that makes sense!. Anyway here is the code I got from a tutorial on the net;

  1. <?php
  2. $dbhost='localhost';
  3. $dbusername='root';
  4. $dbname = 'test';
  5. mysql_connect ($dbhost, $dbusername);
  6. mysql_select_db($dbname) or die('Cannot select database');
  7. $query = "SELECT * FROM test4";
  8. $result = mysql_query($query) or die('Error, query failed');
  9. if(!isset($cmd)){
  10.  
  11. while($row = mysql_fetch_array($result)) {
  12.  
  13. $id = $row["id"];
  14. echo "<strong> tom</strong>: {$row['Tom']}</p>";
  15. echo "<strong> id</strong>: {$row['id']}</p>";
  16. echo "<p><a href='?cmd=delete&id=$id'>delete</a></p>" ;
  17. }
  18. }
  19. if($_GET["cmd"]=="delete")
  20. {
  21. $sql = "DELETE FROM test4 WHERE id=$id";
  22. $result = mysql_query($sql);
  23. }
  24.  
  25. ?>

Any help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 37
Reputation: jblacdao is an unknown quantity at this point 
Solved Threads: 3
jblacdao jblacdao is offline Offline
Light Poster

Re: Simple delete problems.

 
0
  #2
Feb 21st, 2007
Try changing your query to:

  1. $sql = "DELETE FROM test4 WHERE id={$_GET['id']}";

Let me know if it works
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 10
Reputation: tomhughes is an unknown quantity at this point 
Solved Threads: 0
tomhughes tomhughes is offline Offline
Newbie Poster

Re: Simple delete problems.

 
0
  #3
Feb 21st, 2007
Cheers it seems to work a lot better at first glance. I'll play around with it more to add some more features and stuff see if everythings ok.
Thank you very much for your help.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 37
Reputation: jblacdao is an unknown quantity at this point 
Solved Threads: 3
jblacdao jblacdao is offline Offline
Light Poster

Re: Simple delete problems.

 
0
  #4
Feb 21st, 2007
Aight! if it helps you can also put in "LIMIT 1" at the end of the query so the statement will only delete up to 1 record. Good luck!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC