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;

$dbname = 'test';
mysql_connect ($dbhost, $dbusername);
mysql_select_db($dbname) or die('Cannot select database');
$query  = "SELECT * FROM test4"; 
$result = mysql_query($query) or die('Error, query failed'); 
while($row = mysql_fetch_array($result)) {
$id = $row["id"];
echo "<strong> tom</strong>:  {$row['Tom']}</p>";
echo "<strong> id</strong>:  {$row['id']}</p>";
echo "<p><a href='?cmd=delete&id=$id'>delete</a></p>" ;
$sql = "DELETE FROM test4 WHERE id=$id";
$result = mysql_query($sql);

Any help is appreciated.

11 Years
Discussion Span
Last Post by jblacdao

Try changing your query to:

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

Let me know if it works


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.


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!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.