使用PHP

时间:2018-02-07 11:27:19

标签: php html mysql

我正在尝试在我正在学校做的网站项目中添加一个delete按钮。几乎像一个论坛,您将删除对主题的回复...我希望能够删除单个注释。目前我可以删除,但它只会删除最近的注释(注释表的底部)。如何获取它,以便delete按钮删除相应行中的注释,而不仅仅是底部注释。

主界面(我相信问题所在)

$sql="SELECT noteID, title, note, timestamp FROM $tbl_name WHERE user='$currentuser' AND subject='$currentsubject'";


$result=mysql_query($sql);
?>

<table align="center" id="notes">
<tr>
<td width="10%"><strong>Title</strong></td>
<td width="10%"><strong>ID</strong></td>
<td width="50%"><strong>Note</strong></td>
<td width="10%"><strong>Time</strong></td>
<td width="10%"><strong>Delete</strong></td>
</tr>

<?php

// Start looping table row
while($rows=mysql_fetch_array($result)){

echo'
<br> 
<tr>
<td>'.$rows['title'].'</td>
<td>'.$rows['noteID'].'</td>
<td>'.$rows['note'].'</td>
<td>'.$rows['timestamp'].'</td> ';
$todelete = $rows['noteID'];
$_SESSION['todelete'] = $todelete;
echo'
<td><form method="post" action="deletenote.php" ><input type="submit" name="submit" value="Delete" </td> 
</tr>

';
}
ob_end_flush()
?>

2 个答案:

答案 0 :(得分:2)

另一种方法是为每个帖子提供一个删除链接:

// Start looping table row
while($rows=mysql_fetch_array($result)){
    echo '<br>';
    echo '<tr>';
    echo '<td>'.$rows['title'].'</td>';
    echo '<td>'.$rows['noteID'].'</td>';
    echo '<td>'.$rows['note'].'</td>';
    echo '<td>'.$rows['timestamp'].'</td>';
    echo '<td><a href="//example.com/deletenote.php?id='.$rows['noteID'].'">Delete</a></td>';
    echo '</tr>';
}

请注意,deletenote.php必须验证用户是否已登录并有权删除帖子。

答案 1 :(得分:0)

首先:关于特定用例(删除注释)的相应代码/查询在哪里?

您正在尝试提交没有任何参数的表单,我假设您正在尝试使用会话变量。但是,此变量将始终设置为最后一次循环迭代。因此,我建议您使用此代码示例:

// Start looping table row
while($rows=mysql_fetch_array($result)){
    echo '<br>';
    echo '<tr>';
    echo '<td>'.$rows['title'].'</td>';
    echo '<td>'.$rows['noteID'].'</td>';
    echo '<td>'.$rows['note'].'</td>';
    echo '<td>'.$rows['timestamp'].'</td>';
    echo '<td><form method="post" action="deletenote.php" ><input type="hidden" name="noteID" value="'.$rows['noteID'].'"><input type="submit" name="submit" value="Delete"></form> </td>';
    echo '</tr>';
}
相关问题