在How can I add my own rating system?之后,我需要一些帮助来将正确的值添加到“指定的”博客帖子中。它在数据库中添加新行,而不是添加已提交rate的正确帖子的值。我知道它为什么要添加新行,但我不知道如何才能将rate值添加到“指定的”博客帖子ID中。
CREATE TABLE IF NOT EXISTS `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`video` varchar(255) NOT NULL,
`title` longtext NOT NULL,
`text` longtext NOT NULL,
`posted_by` longtext NOT NULL,
`rate` int(11) DEFAULT '0',
`clicks` int(11) DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT;
Index.php:
<?php
while($r = mysqli_fetch_assoc($res)){
?>
<div class="card mb-4">
<div class="embed-responsive embed-responsive-16by9">
<iframe style="border:0;" class="embed-responsive-item" width="450" height="240" src="<?php echo $r['video']; ?>" allowfullscreen></iframe>
</div>
<div class="card-body">
<h2 class="card-title"><?php echo $r['title']; ?></h2>
<p><?php echo $r['text']; ?></p>
</div>
<div class="card-footer text-muted"><?php echo $r['posted_by']; ?>
</div>
<form action="../functions/rate.php" method="POST" class="rate-system">
<div class="select">
<select name="slct" id="slct">
<option>Vote for the post</option>
<option name="awful" value="1">1 - Awful</option>
<option name="bad" value="2">2 - Bad</option>
<option name="good" value="3">3 - Good</option>
<option name="cool" value="4">4 - Cool</option>
<option name="awesome" value="5">5 - Awesome</option>
</select>
</div>
<div class="submit-rate">
<input class="rate-submit" type="submit" name="submit" value="Add rating"/>
</div>
</form>
</div>
<?php } ?>
Rate.php (更新于2018年6月23日):
<?php
$servername = "localhost";
$username = "root";
$password = "oregon";
$dbname = "project";
try {
$connection = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset( $_POST['slct']) && $_POST['slct'] === "1") {
$rate = "1";
} else if (isset( $_POST['slct']) && $_POST['slct'] === "2") {
$rate = "2";
} else if (isset( $_POST['slct']) && $_POST['slct'] === "3") {
$rate = "3";
} else if(isset( $_POST['slct']) && $_POST['slct'] === "4") {
$rate = "4";
} else if(isset( $_POST['slct']) && $_POST['slct'] === "5") {
$rate = "5";
} else {
$rate = "0";
}
$clicks = '1';
$id = $_POST['post_id'];
$sql = $connection->prepare("UPDATE posts SET rate = :rate, clicks = :clicks WHERE id = :id");
$sql->bindParam(':id', $id, PDO::PARAM_INT);
$sql->bindParam(':rate', $rate, PDO::PARAM_INT);
$sql->bindParam(':clicks', $clicks, PDO::PARAM_INT);
if ($sql->execute()) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $e->getMessage();
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$connection = null;
?>
https://stackoverflow.com/questions/50975934
复制相似问题