我试图更新评论表中的状态字段,但它失败了。
这是我的表格:
<?php
if ($result['comment_status'] == "approved") {
?>
<form action="" method="post">
<input type="hidden" name="comment_id" value="<?php echo $result['comment_id'] ?>">
<button type="submit" name="disapproved" class="btn btn-primary btn-sm">DisApprove
</button>
</form>
<?php
} else {
?>
<form action="" method="post">
<input type="hidden" name="comment_id" value="<?php echo $result['comment_id'] ?>">
<button type="submit" name="approved" class="btn btn-primary btn-sm">Approve</button>
</form>
<?php
}
?>
这是我的PHP代码(查询):
if (isset($_POST['comment_id']) && is_numeric($_POST['comment_id']) && $_POST['comment_id'] > 0) {
$comment_id = $_POST['comment_id'];
}
if (isset($_POST['disapproved'])) {
$query = $connection->prepare("UPDATE comments SET comment_status = 'approved' WHERE comment_id = $comment_id");
confirm($query->execute(), "Comment approved successfully", "info");
}
if (isset($_POST['approved'])) {
$query = $connection->prepare("UPDATE comments SET comment_status = 'disapproved' WHERE comment_id = $comment_id");
confirm($query->execute(), "Comment disapproved successfully", "info");
}
我不知道为什么不更新
太多的代码重复...
<form action="" method="post">
<input type="hidden" name="comment_id" value="<?php echo $result['comment_id'] ?>">
<input type="hidden" name="status" value="<?=($result['comment_status'] == "approved")?>">
<button type="submit" class="btn btn-primary btn-sm"><?= ($result['comment_status'] == "approved" ? 'DisApprove' : 'Approve' ) ?></button>
</form>
PHP代码。你应该绑定comment_id
和status
if (isset($_POST['comment_id']) & ... && isset($_POST['status'])) {
$comment_id = $_POST['comment_id'];
$status = $_POST['status'] ? 'disapproved' : 'approved';
$query = $connection->prepare("UPDATE comments SET comment_status = :status WHERE comment_id = :comment_id");
$query->bindValue(':comment_id', $comment_id, PDO::PARAM_INT);
$query->bindValue(':status', $status, PDO::PARAM_STR);
confirm($query->execute(), "Comment disapproved successfully", "info");
}
应该工作,试试吧