2013-05-26 35 views
-1

我正在編寫一個PHP代碼,用SQL查詢顯示保存在數據庫中的所有條目。它顯示正確的條目,但現在我試圖做一個允許用戶刪除一個條目的jQuery對話框。
這是我的代碼:
帶MySQL查詢的jQuery對話框

<?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> 

的問題是,當我嘗試刪除的條目,則SQL代碼刪除總是第一個條目,而不是我選擇了一個。
我試着用POST,GET和AJAX來解決這個問題,但是它總是刪除第一個條目。
任何人都可以幫助我嗎?

+1

你不應該echo'ing''showDialogDelete'函數,btw'id'應該是一個參數,沒有理由爲每個條目創建一個特定的函數。仍然BTW,PHP是一種服務器端語言,一旦執行,你不能修改它的客戶端。這就是說,你的邏輯在某個地方是錯誤的。 –

回答

1

有很多方法可以做這種事情,我的只有一個。

,你需要確保的基本過程是:

確保每個顯示的刪除的條目是可識別的。如果每一個條目是有自己的按鈕,你需要沿着線的東西:

<INPUT type='button' value='delete' onClick='deleteFunction(uniqueIdForThisLine)'> 

你deleteFunction然後頒佈一個AJAX調用該刪除相關入口的小PHP文件。

提示:如果你整個包住條目跨度可見:

<SPAN id='uniqueIdForThisLine.spn'>diary text</SPAN> 

這是很容易使該條目消失(顯示:無;),而PHP並在後臺工作。

我懷疑你的問題導致你的所有條目都被賦予了相同的id,或者更可能的是,你偶然會將一個零值傳遞給你的SQL。

檢查你拼寫你的變量名稱正確

回聲你的SQL,以確保它看起來像它應該(這通常表明數據傳球失誤)

設置錯誤所有報告 - 這是一個痛苦的,但它確實執行了良好的編碼練習,並在大多數時間挑選拼寫錯誤的變量名稱。

+0

謝謝!有用! –