我正在尝试创建一个upvote系统,在检查您是否已登录后以及获取用户ID之后,它会检查表中是否有您的用户ID已经包含postid,如果有,那么它意味着它已经被投票了。我只是想知道我的代码中有什么问题,这是用于学习,我不需要任何复杂的事情。
代码:
if (isset($_GET['upvote'])) {
if ($_SESSION["loggedin"] == true) {
$upvoteid = $_GET["upvote"];
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id FROM users WHERE username=".$_SESSION["loggedinusername"];
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$userid = $row["id"];
}
$sql2 = "SELECT userid, postid FROM upvotedposts WHERE userid='".$userid."' AND postid='".$upvoteid."'";
$result2 = $conn->query($sql);
if (!$result2->numrows > 0) {
$sql = "UPDATE posts SET upvotes = upvotes + 1 WHERE id = ".$upvoteid;
if ($conn->query($sql) === TRUE) {
echo "Sucessfully upvoted";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
} else {
echo "Failed;
}
$conn->close();
}
}
当我点击upvote按钮时,它什么都不做。这里的问题是,据我所知,它看起来会起作用,但我可能忘记了一些我不知道或不正确使用的东西。
答案 0 :(得分:0)
您错过了结束"
echo "Failed;
应该是:
echo "Failed";