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
$dbhost='localhost';
$dbusername='root';
$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'); 
if(!isset($cmd)){
 
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>" ;
   }
}
if($_GET["cmd"]=="delete")
{
$sql = "DELETE FROM test4 WHERE id=$id";
$result = mysql_query($sql);
}
 
?>

Any help is appreciated.

Recommended Answers

All 3 Replies

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.