我正在編寫一個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 è dovuto a noi. Riprova più 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> </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">
</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 è dovuto a noi. Riprova più 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"> </center>
</td>
</tr>
<tr>
<td>
<center style="padding: 5px"> </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à: </b><?php echo $priority; ?>
</td>
</tr>
</table>
的問題是,當我嘗試刪除的條目,則SQL代碼刪除總是第一個條目,而不是我選擇了一個。
我試着用POST,GET和AJAX來解決這個問題,但是它總是刪除第一個條目。
任何人都可以幫助我嗎?
你不應該echo'ing''showDialogDelete'函數,btw'id'應該是一個參數,沒有理由爲每個條目創建一個特定的函數。仍然BTW,PHP是一種服務器端語言,一旦執行,你不能修改它的客戶端。這就是說,你的邏輯在某個地方是錯誤的。 –