单击元素时如何更新 table 行 +1?
How to update table row +1 when element is clicked?
我正在建立一个问题投票系统。该站点的访问者可以每天一次或类似地投票,就他们最喜欢的问题进行投票。 单击特定问题的按钮后,如何向 QuestionVotes
行 +1?
我的代码:
<?php
$connection = mysqli_connect('localhost', 'root', '', 'test');
mysqli_set_charset($connection, 'utf8');
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
$sql = "SELECT QuestionHeader, QuestionText, QuestionVotes FROM question ORDER BY QuestionVotes DESC LIMIT 3";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class=\"col-md-4\"><h2>". $row["QuestionHeader"]. "</h2><p>". $row["QuestionText"]. "</p><p><a class=\"btn btn-success\"> " . $row["QuestionVotes"] . "</a></p></div>";
}
} else {
echo "0 results";
}
$connection->close();
?>
我想我必须以某种方式存储 QuestionID,然后在单击按钮时检索它,但我不知道如何做?我如何避免人们对同一个问题投票两次?
那么,您将需要更改您的数据库 table 或创建额外的 table 链接在一起并具有一对多关系,问题 table 是 1 和存储每个 user's vote
的 table 是多边。
每个问题都应该有一个唯一的 ID
像上面那样循环遍历 Questions
table 中的问题。每行都应有一个按钮,单击该按钮会将问题 ID + 用户 ID/(IP 地址 - 如果系统对非注册用户开放)传递给 user's vote
table.
2a。要在每次唯一用户单击 vote
按钮时增加计数,您必须 Fetch
从 user's vote
table 中获取 Count
以了解如何Question ID
存在很多次。
但是,在将数据存储到数据库之前,请检查 user's vote
table 以查看该用户 ID + 问题 ID 是否已经匹配,如果是的话; return 一条消息,告诉用户他们已经对该问题进行了投票(或者你可以花点时间在页面上做一个 if 检查,如果有匹配 - 禁用投票按钮)
$dbname = "DB HERE";
$servername = "HOST HERE";
$username = "DB USER HERE";
$password = "DB PASSWORD HERE";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
if(isset($_GET['id']))
{
///Check to see if user already voted
$result = $conn->query("SELECT * FROM User_Votes where user id = $session_id and question_id = $id");
$row_cnt = $result->num_rows;
if($row_cnt < 1)
{
///SQL to insert vote into Users Votes table
}else
{
//Vote already exists
}
}
// Loop through questions for voting
$result = mysqli_query($conn,"select * from questions");
while($db_questions = mysqli_fetch_object($result))
{
echo $db_questions->question_title;
echo '- <a href="mypage.php?id=$db_questions->question_id">Click to Vote</a>;
}
您将遇到的最大障碍是识别唯一身份用户。最好的办法就是强制注册登录。那是另一个话题的讨论。
尽管如此,您的 table 还需要另外 2 列。
QuestionID
MediumINT (15),无符号,主索引,自动递增。这应该是第一列。
QuestionVoters
文本,NULL。该字段将包含已投票的用户 ID 的 json 编码数组。 array('123', '38', '27', '15')
在您的 While()
循环中检查用户 ID 是否在 QuestionVoters
数组中。
如果存在,则不要给他们投票操作。否则使用按钮构建表单以提交到处理页面。
<?php
// Need to assign the user's ID to a variable ($userID) to pass to the form.
$userID = '123'; // this needs to be handled on your end.
// updated sql to include Id and voters
$sql = "SELECT QuestionID, QuestionHeader, QuestionText, QuestionVotes, QuestionVoters FROM question ORDER BY QuestionVotes DESC LIMIT 3";
while($row = $result->fetch_assoc()) {
$voters = json_decode($row['QuestionVoters'], true); // array of userid's that have voted
IF (in_array($userID, $voters)) {
// user has voted
echo "\n
<div class=\"col-md-4\">
<h2>". $row["QuestionHeader"]. "</h2>
<p>". $row["QuestionText"]. "</p>
<p>" . $row["QuestionVotes"] . "</p>
</div>";
}ELSE{
// user has not voted
echo "\n
<div class=\"col-md-4\">
<form action=\"vote_processing.php\" name=\"voting\" method=\"post\">
<input type=\"hidden\" name=\"qid\" value=\"".$row['QuestionID']."\" />
<input type=\"hidden\" name=\"userid\" value=\"".$userID."\" />
<h2>". $row["QuestionHeader"]. "</h2>
<p>". $row["QuestionText"]. "</p>
<p><button type=\"submit\" value=\"Submit\">" . $row["QuestionVotes"] . "</button></p>
</form>
</div>";
}
}
?>
vote_processing.php(示例)
<?php
IF (isset($_POST['qid'])) {
$qid = htmlspecialchars(strip_tags(trim($_POST['qid']))); // basic sanitization
$userid = htmlspecialchars(strip_tags(trim($_POST['userid']))); // basic sanitization
IF ( (is_int($qid)) && (is_int($userid)) ) { // validate that both are integers
// db connection
$connection = mysqli_connect('localhost', 'root', '', 'test');
mysqli_set_charset($connection, 'utf8');
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
// Get voters array
$sql = "SELECT QuestionVoters FROM question WHERE QuestionID = '".$qid."'";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
IF (!empty($row['QuestionVoters'])) {
// decode users array
$voters = json_decode($row['QuestionVoters'], true);
}ELSE{
$voters = array(); // create array
}
}
mysqli_free_result($result);
// re-validate the userID "is not" in array
IF (!in_array($userid, $voters)) { // note the ! [meaning NOT].
$voters[] = $userid; // add userid to voters array
$qvoters = json_encode($voters); // encode voters array
// update vote
$sql_upd = "UPDATE question SET QuestionVotes = QuestionVotes + 1, QuestionVoters = $qvoters WHERE QuestionID = '".$qid."'";
$upd_result = $connection->query($sql_upd);
}
}
mysqli_close($connection);
}
}
// redirct back to previous page
?>
我正在建立一个问题投票系统。该站点的访问者可以每天一次或类似地投票,就他们最喜欢的问题进行投票。 单击特定问题的按钮后,如何向 QuestionVotes
行 +1?
我的代码:
<?php
$connection = mysqli_connect('localhost', 'root', '', 'test');
mysqli_set_charset($connection, 'utf8');
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
$sql = "SELECT QuestionHeader, QuestionText, QuestionVotes FROM question ORDER BY QuestionVotes DESC LIMIT 3";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class=\"col-md-4\"><h2>". $row["QuestionHeader"]. "</h2><p>". $row["QuestionText"]. "</p><p><a class=\"btn btn-success\"> " . $row["QuestionVotes"] . "</a></p></div>";
}
} else {
echo "0 results";
}
$connection->close();
?>
我想我必须以某种方式存储 QuestionID,然后在单击按钮时检索它,但我不知道如何做?我如何避免人们对同一个问题投票两次?
那么,您将需要更改您的数据库 table 或创建额外的 table 链接在一起并具有一对多关系,问题 table 是 1 和存储每个 user's vote
的 table 是多边。
每个问题都应该有一个唯一的 ID
像上面那样循环遍历
Questions
table 中的问题。每行都应有一个按钮,单击该按钮会将问题 ID + 用户 ID/(IP 地址 - 如果系统对非注册用户开放)传递给user's vote
table.2a。要在每次唯一用户单击
vote
按钮时增加计数,您必须Fetch
从user's vote
table 中获取Count
以了解如何Question ID
存在很多次。但是,在将数据存储到数据库之前,请检查
user's vote
table 以查看该用户 ID + 问题 ID 是否已经匹配,如果是的话; return 一条消息,告诉用户他们已经对该问题进行了投票(或者你可以花点时间在页面上做一个 if 检查,如果有匹配 - 禁用投票按钮)$dbname = "DB HERE"; $servername = "HOST HERE"; $username = "DB USER HERE"; $password = "DB PASSWORD HERE"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); if(isset($_GET['id'])) { ///Check to see if user already voted $result = $conn->query("SELECT * FROM User_Votes where user id = $session_id and question_id = $id"); $row_cnt = $result->num_rows; if($row_cnt < 1) { ///SQL to insert vote into Users Votes table }else { //Vote already exists } } // Loop through questions for voting $result = mysqli_query($conn,"select * from questions"); while($db_questions = mysqli_fetch_object($result)) { echo $db_questions->question_title; echo '- <a href="mypage.php?id=$db_questions->question_id">Click to Vote</a>; }
您将遇到的最大障碍是识别唯一身份用户。最好的办法就是强制注册登录。那是另一个话题的讨论。
尽管如此,您的 table 还需要另外 2 列。
QuestionID
MediumINT (15),无符号,主索引,自动递增。这应该是第一列。
QuestionVoters
文本,NULL。该字段将包含已投票的用户 ID 的 json 编码数组。 array('123', '38', '27', '15')
在您的 While()
循环中检查用户 ID 是否在 QuestionVoters
数组中。
如果存在,则不要给他们投票操作。否则使用按钮构建表单以提交到处理页面。
<?php
// Need to assign the user's ID to a variable ($userID) to pass to the form.
$userID = '123'; // this needs to be handled on your end.
// updated sql to include Id and voters
$sql = "SELECT QuestionID, QuestionHeader, QuestionText, QuestionVotes, QuestionVoters FROM question ORDER BY QuestionVotes DESC LIMIT 3";
while($row = $result->fetch_assoc()) {
$voters = json_decode($row['QuestionVoters'], true); // array of userid's that have voted
IF (in_array($userID, $voters)) {
// user has voted
echo "\n
<div class=\"col-md-4\">
<h2>". $row["QuestionHeader"]. "</h2>
<p>". $row["QuestionText"]. "</p>
<p>" . $row["QuestionVotes"] . "</p>
</div>";
}ELSE{
// user has not voted
echo "\n
<div class=\"col-md-4\">
<form action=\"vote_processing.php\" name=\"voting\" method=\"post\">
<input type=\"hidden\" name=\"qid\" value=\"".$row['QuestionID']."\" />
<input type=\"hidden\" name=\"userid\" value=\"".$userID."\" />
<h2>". $row["QuestionHeader"]. "</h2>
<p>". $row["QuestionText"]. "</p>
<p><button type=\"submit\" value=\"Submit\">" . $row["QuestionVotes"] . "</button></p>
</form>
</div>";
}
}
?>
vote_processing.php(示例)
<?php
IF (isset($_POST['qid'])) {
$qid = htmlspecialchars(strip_tags(trim($_POST['qid']))); // basic sanitization
$userid = htmlspecialchars(strip_tags(trim($_POST['userid']))); // basic sanitization
IF ( (is_int($qid)) && (is_int($userid)) ) { // validate that both are integers
// db connection
$connection = mysqli_connect('localhost', 'root', '', 'test');
mysqli_set_charset($connection, 'utf8');
if (!$connection) {
die("Database connection failed: " . mysqli_error());
}
// Get voters array
$sql = "SELECT QuestionVoters FROM question WHERE QuestionID = '".$qid."'";
$result = $connection->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
IF (!empty($row['QuestionVoters'])) {
// decode users array
$voters = json_decode($row['QuestionVoters'], true);
}ELSE{
$voters = array(); // create array
}
}
mysqli_free_result($result);
// re-validate the userID "is not" in array
IF (!in_array($userid, $voters)) { // note the ! [meaning NOT].
$voters[] = $userid; // add userid to voters array
$qvoters = json_encode($voters); // encode voters array
// update vote
$sql_upd = "UPDATE question SET QuestionVotes = QuestionVotes + 1, QuestionVoters = $qvoters WHERE QuestionID = '".$qid."'";
$upd_result = $connection->query($sql_upd);
}
}
mysqli_close($connection);
}
}
// redirct back to previous page
?>