我的删除表单有问题。当您按下删除按钮时,它将始终删除最后插入的行而不是您要删除的行。 当我按下删除时,我正在使用jQuery Ajax SendForm函数对div进行更新。它在添加该功能之前有效。它只是删除不能与SendForm一起使用;你可以做更新,它的工作正常。
<?php
foreach ($pdo->query( 'SELECT * FROM Commenta order by commentID desc;' ) as $row) :
$luck = $row ['commentID'];
echo "<tr>";
echo "<td><i class='fa fa-eye w3-blue w3-padding-tiny'></i></td>";
echo "<td>".$row['alias']."</td>";
echo "<td>";
foreach($pdo->query( 'select realName from SubPlace, CommentSubPlace where SubPlace.name = CommentSubPlace.name and CommentSubPlace.commentID = '.$luck.';' ) as $brow){;
echo $brow['realName']."</br>";
};
echo "</td>";
echo "<td>".$row['grade']." / 5 </td>";
echo "<td>".$row['kommentar']."</td>";
echo "<td>".$row['date']."</td>";
?>
<td class="comment-delete">
<form id="cucdel">
<input type="hidden" name="commentID" value="<?php echo $row['commentID']; ?>">
<button type="button" onclick="SendForm('comments', 'comments', 'cucdel');">radera</button>
</form>
</td>
</tr>
<?php
endforeach;
?>
代码的删除部分:
<?php
if (isset($_POST['commentID'])) {
$sql = "DELETE FROM Commenta WHERE commentID = :commentID";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':commentID', $_POST['commentID'], PDO::PARAM_INT);
$stmt->execute();
}
?>
答案 0 :(得分:1)
这一行
foreach($pdo->query( 'select realName from SubPlace, CommentSubPlace where SubPlace.name = CommentSubPlace.name and CommentSubPlace.commentID = '.$luck.';' ) as $brow){;
虽然没有语法错误 - 但它的半冒号没有任何意义。如果改为:
,你和你身后的程序员可以更清楚地了解这一行// protect yourself from bad data
$luckint = (integer) $luck;
// lose the confusing and unnecessary semi-colon here
// prepare the query separately
$query = 'select realName from SubPlace, CommentSubPlace where SubPlace.name = CommentSubPlace.name and CommentSubPlace.commentID = '.$luckint;
// this line *can not* end in a semi-colon - find your error log
foreach ($pdo->query( $query ) as $brow) {
回答问题
始终删除最后一个的原因是表单ID 。您始终在每个删除表单中打印相同的ID。 id必须是唯一的 - 使表单ID如下:
<form id="cucdel<?php echo $row['commentID']; ?>">
<input type="hidden" name="commentID" value="<?php echo $row['commentID']; ?>">
<button type="button" onclick="SendForm('comments', 'comments', 'cucdel<?php echo $row['commentID']; ?>');">radera</button>
</form>
然后更新