Hello Stack溢出社区。 p>
到目前为止,我已经针对评级系统提出了两个主题,并且很高兴在您的帮助下实现了这一目标。它工作正常,只有每个人都可以投票,因此我在每次投票后都将客户的IP添加到数据库表“ votes”中。我想帮助我进行如下检查:1)如果客户已经投票通过,则不将其评分存储在数据库中。
我又如何避免插入postid,而ip是否已经存在于'votes'中呢? 例如,我对ID为2的帖子投了一次票,它创建了: -ID:1 -postid:2 -ip:客户端的IP
当对同一帖子再次投票时,它会创建相同的信息,因此这是无用的,应将其屏蔽。
投票表结构:
CREATE TABLE IF NOT EXISTS `votes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`postid` int(11) NOT NULL,
`ip` varchar(15) DEFAULT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
已更新:/ Full Rate.php /
<?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);
$postid = $_POST['post_id'];
$ip = $_SERVER['REMOTE_ADDR'];
$sql = $connection->prepare("SELECT postid, ip FROM votes WHERE ip = :ip");
$sql->bindParam(':ip', $ip, PDO::PARAM_STR);
$sql->execute();
if($sql->rowCount() > 0){
die("You already voted");
}
$postid = $_POST['post_id'];
$ip = $_SERVER['REMOTE_ADDR'];
$sql = $connection->prepare("INSERT INTO votes (postid, ip) VALUES (:postid, :ip)");
$sql->bindParam(':postid', $postid);
$sql->bindParam(':ip', $ip);
$sql->execute();
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";
}
$id = $_POST['post_id'];
$rating = $_POST['rate'];
$clicks = $_POST['clicks'];
$total_clicks = $clicks + '1';
$max_rate = $rating + $rate;
if($_POST['slct'] == "0") {
$clicks = $_POST['clicks'];
$total_clicks = $clicks + '0';
}
$sql = $connection->prepare("UPDATE posts SET rate = :max_rate, clicks = :total_clicks WHERE id = :id");
$sql->bindParam(':id', $id, PDO::PARAM_INT);
$sql->bindParam(':max_rate', $max_rate, PDO::PARAM_INT);
$sql->bindParam(':total_clicks', $total_clicks, PDO::PARAM_INT);
if ($sql->execute()) {
header('Location: ../index.php');
} else {
echo "Error updating record: " . $e->getMessage();
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$connection = null;
?>
致谢。
答案 0 :(得分:0)
您可以使用SELECT
查询来检查数据库中是否已存在IP地址。
$ip = $_SERVER['REMOTE_ADDR'];
$sql = $connection->prepare("SELECT postid, ip FROM votes WHERE ip = :ip");
$sql->bindParam(':ip', $id, PDO::PARAM_INT);
$sql->execute();
if($sql->rowCount() > 0){
// I prefer to use the rowCount() but also a fetch will be ok for your task
echo 'You already voted.';
} else { // do your saving tasks here }
答案 1 :(得分:0)
在发布整个代码之前,我希望您尝试一下。如果可行,我将在第二部分中发布UPDATE
查询
<?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);
$sql = $connection->prepare("SELECT * FROM votes");
if ($sql->execute()) {
$row_votes = $sql->fetch();
if ($row_votes['postid'] == $_POST['post_id'] && $row_votes['ip'] == $_SERVER['REMOTE_ADDR']) {
echo "You've already voted for this post. You cannot vote again.";
} else {
$sql = $connection->prepare("INSERT INTO votes (postid, ip) VALUES (:postid, :ip)");
$sql->bindParam(':postid', $postid);
$sql->bindParam(':ip', $ip);
$postid = $_POST['post_id'];
$ip = $_SERVER['REMOTE_ADDR'];
if ($sql->execute()) {
echo "INSERT was successeful";
} else {
echo "INSERT was unseccessful";
}
}
} else {
echo "Could not execute query";
}
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$connection = null;
?>