954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple delete problems.

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.

tomhughes
Newbie Poster
10 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

Try changing your query to:

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


Let me know if it works

jblacdao
Light Poster
37 posts since Jan 2007
Reputation Points: 12
Solved Threads: 3
 

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.

tomhughes
Newbie Poster
10 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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!

jblacdao
Light Poster
37 posts since Jan 2007
Reputation Points: 12
Solved Threads: 3
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You