I have a problem with a delete function. I have a script that lists all database container and a little script that offer the possibility to delete the row of that db. When i click on the delete link of a row, the delete function should get the id of the row that i want to delete and delete it.
The script looks like this:

When i click on id 2 for delete, the script deletes the last row, id 3 in my example. I dunno what i'm doing wrong :-[

Can you help me, Please?

Thank you in advance!

skywalker2208

03-04-2010, 11:23 PM

Your problem is here

while ($row = mysql_fetch_array($result)){
$id = $row['ID'];

mysql_fetch_array only returns numeric keys for the array. What you want to use is mysql_fetch_assoc then you do $row['ID'].

If you do a print on $id you will see that it is empty.

Also with the line below you should google sql injection

$sql = "delete from ndeshje WHERE ID = '$_GET[id]'";

kleidi

03-04-2010, 11:32 PM

Your problem is here

while ($row = mysql_fetch_array($result)){
$id = $row['ID'];

mysql_fetch_array only returns numeric keys for the array. What you want to use is mysql_fetch_assoc then you do $row['ID'].

If you do a print on $id you will see that it is empty.

Also with the line below you should google sql injection

$sql = "delete from ndeshje WHERE ID = '$_GET[id]'";

Thank you for your reply.
I'm new on php/mysql, can u help me with the complete string for what you said about mysql_fetch_assoc, PLEASE?
About sql injection, i'm still reading and learning, anyway, can u give me any string mode example for my case on protection from sql injections, PLEASE?

}
at the end of the page and this resolved my problem. Seems that this query, replaced all the id's in delete query page with the last id on the list.
Now it works great. Thank you for your reply anyway ;)

tomws

03-05-2010, 01:46 AM

mysql_fetch_array only returns numeric keys for the array.

Incorrect. http://us2.php.net/mysql_fetch_array

skywalker2208

03-05-2010, 02:34 AM

Incorrect. http://us2.php.net/mysql_fetch_array

Thanks for the heads up. I guess I never read the whole description on php.net because it books it has always said use this for numerical keys and use the assoc one for associative keys.