944,035 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 1373
  • PHP RSS
Feb 21st, 2007
0

Simple delete problems.

Expand Post »
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;

PHP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhughes is offline Offline
10 posts
since Feb 2007
Feb 21st, 2007
0

Re: Simple delete problems.

Try changing your query to:

PHP Syntax (Toggle Plain Text)
  1. $sql = "DELETE FROM test4 WHERE id={$_GET['id']}";

Let me know if it works
Reputation Points: 12
Solved Threads: 3
Light Poster
jblacdao is offline Offline
37 posts
since Jan 2007
Feb 21st, 2007
0

Re: Simple delete problems.

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tomhughes is offline Offline
10 posts
since Feb 2007
Feb 21st, 2007
0

Re: Simple delete problems.

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!
Reputation Points: 12
Solved Threads: 3
Light Poster
jblacdao is offline Offline
37 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Creating a login page in PHP
Next Thread in PHP Forum Timeline: How can i pick a random sixdigit numbers in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC