Q:

jQuery Dialog with MySQL query

I'm making a PHP code that shows all entry saved in a database, with a SQL query. It shows correctly the entry, but now I'm trying to do a jQuery Dialog that allow the user to delete one entry.
This is my code:
<?php
    $username="HIDDEN";
    $password="HIDDEN";
    $database="HIDDEN";

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Non riesco a scaricare la lista degli eventi del Diario. L'errore &egrave; dovuto a noi. Riprova pi&ugrave; tardi. Ci scusiamo per il disagio.");
    $query= ("SELECT * FROM diary WHERE Username = '{$_SESSION['Username']}' AND NOT (folder = 'c') ORDER BY " . $_GET['sort']);
    $result= mysql_query($query);

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

    $type = mysql_result($result,$i,"type");
    $create_date = mysql_result($result,$i,"create_date");
    $priority = mysql_result($result,$i,"priority");
    $date = mysql_result($result,$i,"date");
    $materia = mysql_result($result,$i,"materia");
    $descr = mysql_result($result,$i,"descr");
    $id = mysql_result($result,$i,"id");

    ?>
<!-- finestra eliminazione -->
<div id="dialog-delete<?= $id; ?>" title="Sei sicuro di voler spostare nel cestino <?php echo($type . ' di ' . $materia); ?>" style="display: none;">
<div style="margin: 20px">
<img src="http://cdn1.iconfinder.com/data/icons/DarkGlass_Reworked/128x128/apps/konsolekalendar.png" alt="" height="127" width="120" style="margin-right: 30px; float: left" /><span class="auto-style4">Sei sicuro di voler eliminare <?php echo $type . ' di ' .$materia; ?>?<br>
</span><span class="auto-style5">Stai per spostare nel cestino questo 
elemento<br>creato in data <?php echo $create_date; ?>.<br><br></span>
<table style="width: 70%">
    <tr>
        <td style="width: 50%">
        <button onclick="javascript:window.close();" style="width: 96px; height: 28px;">Annulla</button>&nbsp;</td>
        <td class="auto-style6" style="width: 50%">
        <form name="form" method="post">
        <input type="submit" style="width: 95px; height: 28px; float: right" name="deletediary" value="Elimina">&nbsp;
        </form>
        </td>
        <?php

        if(isset($_POST['deletediary'])){

        $username="HIDDEN";
        $password="HIDDEN";
        $database="HIDDEN";

        mysql_connect(localhost,$username,$password);
        @mysql_select_db($database) or die( "Non riesco a leggere l'elemento. L'errore &egrave; dovuto a noi. Riprova pi&ugrave; tardi. Ci scusiamo per il disagio.");
        $query= ("UPDATE diary SET folder = 'c' WHERE (Username = '{$_SESSION['Username']}') AND (id = '".$id."') LIMIT 1");
        $result= mysql_query($query);

        $num = mysql_numrows($result);

        mysql_close();

        echo("Evento diario spostato correttamente nel cestino.");

        }
        ?>
    </tr>
</table>
</div>
</div>
    <?php
    echo('  <script type="text/javascript">
    function showDialogDelete'.$id.'()
    {
        $( "#dialog-delete'.$id.'" ).dialog({
                width: 650,
                height: 250,
                modal: true,
                open: function(event, ui)
                {

                }
            });
    }
</script>');
    ?>
    <table style="margin-bottom: 5px; width: 100%" cellspacing="10px">
    <tr>
    <td rowspan="2" style="width: 80px">
    <table cellpadding="10px" style="width: 70px; height: 60px">
    <tr>
    <td>
    <center style="padding: 5px">&nbsp;&nbsp;</center>
    </td>
    </tr>
    <tr>
    <td>
    <center style="padding: 5px">&nbsp;&nbsp;</center>
    </td>
    </tr>
    </table>
    </td>
    <td>
    <b><?php echo $type . ' di ' . $materia ?></b><div style="float: right; color: #585858; font-size: 16pt"><span class="tooltip" onmouseover="tooltip.pop(this, '<a href=read.php?id=<?= $id?>&type=diary>Apri</a><br/><a href=\'javascript:void(null);\' onclick=\'showDialogDelete<?= $id; ?>();\'>Elimina</a>')" style="color: <?= $_SESSION['accent'] ?>">...</span></div>
    </td>
    </tr>
    <tr>
    <td>
    <b>Priorit&agrave;: </b><?php echo $priority; ?>
    </td>
    </tr>
    </table>

The problem is that when I try to delete an entry, the SQL code delete always the first entry, and not the one I selected.
I've tried to solve this problem also with POST, GET and AJAX, but it always delete the first entry.
Can anyone help me, please?

A:

There are lots of ways to do this sort of thing and mine is only one.
The basic process you need to ensure is:
Ensure each displayed deletable entry is identifiable.  If each entry is to have its own button, you need something along the lines of:
<INPUT type='button' value='delete' onClick='deleteFunction(uniqueIdForThisLine)'>

Your deleteFunction then enacts an ajax call to a small PHP file that deletes the relevant entry. 
Hint:  if you wrap the whole entry in a span vis: 
<SPAN id='uniqueIdForThisLine.spn'>diary text</SPAN>

It is very easy to make the entry vanish (display: none;) while the php does the work in the background.
I suspect that your problem results either from all your entries being given the same id or, perhaps more likely, you are accidentally passing a zero value for the id to your SQL.
Check you have spelled your variable names properly
Echo your sql to ensure it looks like it should (this usually shows data passing errors)
Set error reporting to all - this is a pain but it does enforce good coding practice and picks up the misspelled variable names most of the time.

