这几天我遇到了麻烦。还在学习..无论如何 - 通过这个输入
form action='votes.php' method='post'>
<input type='hidden' name='WHATNAME' value='WHAT?'/>
<input class='voteUp' type='submit' name='submitUp' value='+'/>
</form>
我想更新表格!我想更新用+ 1提交的'voteup',只有那个。 HEre是表Answers
CREATE TABLE IF NOT EXISTS `answers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`answer` text,
`questionId` int(11) NOT NULL,
`user` varchar(255),
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`ip` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
`voteup` int(11) NOT NULL,
PRIMARY KEY (`id`)
);
这是第二个表格问题
CREATE TABLE IF NOT EXISTS `questions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`question` text NOT NULL,
`user` varchar(255) NOT NULL,
`datum` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
所以,有了这个,我有一个表格问题,每个问题都有很多答案。所以我想,我需要让有人可以为他认为正确的每个答案投票(投票)。到目前为止,我只投了一个答案才能获得成功,如果我给出第二个答案,它会给我一些奇怪的多次和双次计数。这是来自文件Votes.php的mysql查询更新我的投票,但只有当我有一个问题!
if (isset($_POST['submitUp'])) {
$ip = $_SERVER['REMOTE_ADDR'];
$sql="INSERT INTO answers
SET voteup = voteup + 1,
ip = '$ip'
";
这里是选择计数脚本,显示我有多少票
$ip = $_SERVER['REMOTE_ADDR'];
$results = mysqli_query($con,"SELECT SUM(voteup) as voteup FROM answers ");
while($row = mysqli_fetch_array($results))
{
echo $row['voteup'];
}
如何针对具有不同问题ID的每个答案更新voteup字段。我尝试了几个组合,但没有!我希望我很清楚。谢谢!!!
答案 0 :(得分:0)
您还需要将问题ID发布到脚本中:
<form action='votes.php' method='post'>
<input type='hidden' name='question_id' value='<?php echo $question_id; ?>'/>
<input type='hidden' name='answer_id' value='<?php echo $answer_id; ?>'/>
<input class='voteUp' type='submit' name='submitUp' value='+'/>
</form>
Votes.php应根据这些值进行更新(同时查找保护您的查询:http://www.php.net/manual/en/mysqli.real-escape-string.php):
if (isset($_POST['submitUp'])) {
$ip = $_SERVER['REMOTE_ADDR'];
$question_id = mysqli_real_escape_string($connection, $_POST['question_id']);
$anwer_id = mysqli_real_escape_string($connection, $_POST['answer_id']);
$sql="UPDATE answers
SET voteup = voteup + 1,
ip = '$ip'
WHERE id = $answer_id
AND questionId = $question_id
";
}
请注意,在这种情况下,IP仅反映最后一个upvote的IP。要记录每个upvote的IP,您需要一个单独的表来存储upvote IP。