【发布时间】:2019-10-29 03:30:29
【问题描述】:
我正在尝试更新数据库中的记录。记录和一些字段显示在表格的页面上。在网页中按下记录旁边的编辑按钮后,它会将用户带到一个新页面,其中该记录的信息显示在一个新页面上,该记录的数据位于文本框中。
我不确定是什么问题,一开始我以为是变量名,但现在不这么认为了。
<form action="../models/updateReview.php" method="POST">
<div class="row">
<div class="input-field">
<input type="hidden" name="reviewId" value="<?php echo $indStmt['revid']; ?>">
</div>
</div>
<div class="row">
<div class="input-field">
<input type="text" name="author" class="validate" value="<?php echo $indStmt['author']; ?>">
<label class="author">Author</label>
</div>
</div>
<div class="row">
<div class="input-field">
<input type="text" name="location" class="validate" value="<?php echo $indStmt['location']; ?>">
<label class="location">Location</label>
</div>
</div>
<div class="row">
<div class="input-field">
<input type="text" name="rating" class="validate" value="<?php echo $indStmt['rating']; ?>">
<label class="rating">Rating(1-5)</label>
</div>
</div>
<div class="row">
<div class="input-field">
<input type="text" name="review" class="validate" value="<?php echo $indStmt['review']; ?>">
<label class="review">Review</label>
</div>
</div>
<div class="row center">
<button class="btn-large black" type="submit">Edit</button>
</div>
</form>
<?php
$reviewId = trim(filter_input(INPUT_POST, 'reviewId', FILTER_SANITIZE_STRING));
$reviewName = trim(filter_input(INPUT_POST, 'author', FILTER_SANITIZE_STRING));
$reviewLocation = trim(filter_input(INPUT_POST, 'location', FILTER_SANITIZE_STRING));
$reviewRating = trim(filter_input(INPUT_POST, 'rating', FILTER_SANITIZE_STRING));
$reviewMessage = trim(filter_input(INPUT_POST, 'review', FILTER_SANITIZE_STRING));
if (empty($reviewName) || empty($reviewLocation) || empty($reviewRating) || empty($reviewMessage)){
echo "Invalid Data Entry. Please check all field and try again";
}else {
require('dbConnect.php');
$statement = $connection->prepare('UPDATE tblreviews SET author = :author, location = :location, rating = :rating, review = :review WHERE revid = :reviewId');
$statement->bindValue(':author', $reviewName);
$statement->bindValue(':location', $reviewLocation);
$statement->bindParam(':rating', $reviewRating);
$statement->bindParam(':review', $reviewMessage);
$statement->execute();
header('Location: ../views/reviews.php');
}
?>
第一个代码块是按下编辑按钮时出现的表单页面。第二个是我尝试更新实际记录的过程。它通过执行并将用户带回主页,就好像它可以正常工作一样,但实际上并没有更新记录。
【问题讨论】:
-
欢迎来到 StackOverflow。看起来您缺少 reviewId 的绑定。您还应该添加检查以查看是否修改了任何行。
标签: php html mysql materialize