Delete row don't work correctly

Hello to everyone!

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:

  • Main page:
<?php
session_start();

if(!isset($_SESSION['loggedin'])) {
   header('Location: '.$domain.'index.php?error=1');
   exit();
}
?>
<html>
<head>
<script type="text/javascript">
var form_id;
function confirm_delete(go_url)
{
var answer = confirm("Jeni te sigurte per fshirjen e ketij evenimenti?");
if (answer)
{
location=go_url;
}
}
</script>
</head>

<body>
<?php
include '/includet/variabla.php';
include (BPATH_ADM . 'includet/dbconfig.php');
include (BPATH_ADM . 'includet/dblidhja.php');
$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ora`,`data`";
$result=mysql_query($query);
$num=mysql_numrows($result);

mysql_close();
?>
<br /><br /><center><div class="ndeshjeshfaq">
<table width="598" border="0" align="center" class="ndeshjekoka">
  <tr>
    <td width="25" class="ndshfaqid">ID</td>
    <td width="35" class="ndshfaqsporti">Sporti</td>
    <td width="265" class="ndshfaqndeshja">Ndeshja</td>
    <td width="50" class="ndshfaqmenyra">Menyra</td>
    <td width="50" class="ndshfaqora">Ora</td>
    <td width="90" class="ndshfaqdata">Data</td>
    <td width="110" class="ndshfaqmod">X - Mod</td>
  </tr>
</table></center>


<?php
$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"ID");
$ndeshja=mysql_result($result,$i,"ndeshja");
$ora=mysql_result($result,$i,"ora");
$data=mysql_result($result,$i,"data");
$menyra=mysql_result($result,$i,"menyra");
$sporti=mysql_result($result,$i,"sporti");
?>
<center>
<table width="598" border="0" class="ndeshjetabela">
  <tr>
    <td width="25" class="ndshfaqid"><?php echo $id; ?></td>
    <td width="35" class="ndshfaqsporti"><img src="..<?php echo $sporti; ?>" width="13"></td>
    <td width="265" class="ndshfaqndeshja"><?php echo $ndeshja;  ?></td>
    <td width="50" class="ndshfaqmenyra"><?php echo $menyra;  ?></td>
    <td width="50" class="ndshfaqora"><?php echo $ora;  ?></td>
    <td width="90" class="ndshfaqdata"><?php echo $data;  ?></td>
    <?php
include (BPATH_ADM . 'includet/dbconfig.php');
include (BPATH_ADM . 'includet/dblidhja.php');
$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ID`";
$result=mysql_query($query);
while ($row = mysql_fetch_array($result)){
$id = $row['ID'];

}
?&gt;
    &lt;td width="110" class="ndshfaqmod"&gt;&lt;a href="#" onClick="confirm_delete('modulet/ndeshje/fshij.php?fshij=true&id=&lt;?php echo $id;?&gt;');"&gt;Fshije&lt;/a&gt; - &lt;a href="link-for-edit-entry.php"&gt;Mod&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;&lt;/center&gt;
    
&lt;/div&gt;
&lt;?php
$i++;
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;
  • Delete Page:
&lt;html&gt;
&lt;body&gt;
&lt;?php
include '/includet/variabla.php';
include (BPATH_ADM . 'includet/dbconfig.php');
include (BPATH_ADM . 'includet/dblidhja.php');
$sql = "delete from ndeshje WHERE ID = '$_GET[id]'";
$result = mysql_query($sql);

//print $sql;

if (!$result) {
   echo "&lt;div align ='center' class='error'&gt;Fshirja e ndeshjes deshtoi!";
   echo "&lt;br&gt;";
   echo "&lt;br&gt;";
   echo '&lt;form&gt;&lt;input type="button" class="buton" value="Kthehu Mbrapa"
ONCLICK="history.go(-1)"&gt;&lt;/form&gt;';
   } else {
    echo "&lt;div align ='center' class='header2'&gt;Ndeshja u fshi me sukses!";
   echo "&lt;br&gt;";
   echo "&lt;br&gt;";
   echo '&lt;form&gt;&lt;input type="button" class="buton" value="Kthehu Mbrapa"
ONCLICK="history.go(-1)"&gt;&lt;/form&gt;';
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;

But the problem is, that when i click on the link of the row that i want to delete, the delete scripts delete another row, the last one, ex:

I have three rows ordered by time,date:

id time date name
2 20:10 04.03.2010 name 2
1 20:20 04.03.2010 name 1
3 20:30 04.03.2010 name 3

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!

I think it might be because where you have:

    &lt;td width="50" class="ndshfaqmenyra"&gt;&lt;?php echo $menyra;  ?&gt;&lt;/td&gt;

    &lt;td width="50" class="ndshfaqora"&gt;&lt;?php echo $ora;  ?&gt;&lt;/td&gt;

    &lt;td width="90" class="ndshfaqdata"&gt;&lt;?php echo $data;  ?&gt;&lt;/td&gt;

    &lt;?php

include (BPATH_ADM . 'includet/dbconfig.php');

include (BPATH_ADM . 'includet/dblidhja.php');

$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ID`";

$result=mysql_query($query);

while ($row = mysql_fetch_array($result)){

$id = $row['ID'];



}

?&gt;

    &lt;td width="110" class="ndshfaqmod"&gt;&lt;a href="#" onClick="confirm_delete('modulet/ndeshje/fshij.php?fshij=true&id=&lt;?php echo $id;?&gt;');"&gt;Fshije&lt;/a&gt; - &lt;a href="link-for-edit-entry.php"&gt;Mod&lt;/a&gt;&lt;/td&gt;

  &lt;/tr&gt;

&lt;/table&gt;&lt;/center&gt;

you should remove:


    &lt;?php

include (BPATH_ADM . 'includet/dbconfig.php');

include (BPATH_ADM . 'includet/dblidhja.php');

$query="SELECT * FROM `ndeshje` ORDER BY `ndeshje`.`ID`";

$result=mysql_query($query);

while ($row = mysql_fetch_array($result)){

$id = $row['ID'];



}

?&gt;

That bit of the code will just iterate all through every row in the table and make ID the value of the last record.

You already have the value of ID so you don’t need to get it again.

If you really did want to the correct sql query would be

$query="SELECT * FROM `ndeshje` WHERE `ndeshje`.`ID` = $id";

And as you can see you already have done this information anyway

Yeap… Problem resolved. I moved

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

}

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 and for helping me :wink: