我现在想要花2个小时来弄清楚为什么我的桌子不会更新,而对于上帝的爱,我无法弄明白。这是一个简单的民意调查设置。 我已经取代" UPDATE民意调查"用"更新评论"它会毫无问题地更新评论。然而民意调查只是不起作用,所以我猜问题存在于数据库中,但就我所能得到的而言。也许有人可以帮我解决这个问题?
if (@$_POST['vote']) {
$check_votes = mysql_query ("SELECT votes FROM votes WHERE voter='$user' AND vote_subject='$subject'");
$numrows_votes = mysql_num_rows($check_votes);
if ($numrows_votes == 1) {
$error = "You already voted on this poll.";
}
else {
$check_polls = mysql_query ("SELECT * FROM polls WHERE poll_subject='$subject'");
while($row_polls = mysql_fetch_array($check_polls)) {
$vote1 = $row_polls['vote_one'];
$vote2 = $row_polls['vote_two'];
$numvote = 1;
$newvote1 = $vote1 + $numvote;
$newvote2 = $vote2 + $numvote;
$polloption = $_POST['choice'];
if ($polloption == "") {
$error = "You didn't select any option.";
}
else if ($polloption == "one") {
mysqli_query("UPDATE polls SET vote_one='$newvote1' WHERE poll_subject='$subject'");
}
else {
mysqli_query("UPDATE polls SET vote_two='$newvote2' WHERE poll_subject='$subject'");
}
}
}
}
这是表单代码:
<form action="" method="POST">
<h4>What question would you like to ask?</h4>
<input type='radio' name='choice' value='one'>
<?php echo "$newvote1"; ?>
<br>
<input type='radio' name='choice' value='two'>
<?php echo "$newvote2"; ?>
<p class="buttons">
<input type="submit" name="vote" value="VOTE">
</p>
</form>
答案 0 :(得分:0)
正如上面@jeroen的评论中所提到的,您正在混合数据库API。但是,你应该做的是抛弃它们并使用更高级别的API,如PDO。
mysql_*
函数已经不支持5年了,并且将完全从下一版本的PHP中删除。如果使用正确,mysqli_*
函数可以正常使用,但PDO提供了许多便捷方法,并且还允许您更轻松地更改基础数据库。
这是使用预处理语句快速用PDO重写的代码片段。假设$db
为set up correctly,则完全未经测试但应该有效。如果您计划对数据库执行任何操作,则应阅读准备好的语句并不断使用它们。
if (isset($_POST['vote'])) {
$stmt = $db->prepare("SELECT COUNT(*) FROM votes WHERE voter=? AND vote_subject=?");
$stmt->execute(array($user, $subject));
if ($stmt->fetchColumn() > 0) {
$error = "You already voted on this poll.";
} else {
$stmt = $db->prepare("SELECT * FROM polls WHERE poll_subject=?");
$stmt->execute(array($subject));
while($row_polls = $stmt->fetch(PDO::FETCH_ASSOC)) {
$vote1 = $row_polls['vote_one'];
$vote2 = $row_polls['vote_two'];
$numvote = 1;
$newvote1 = $vote1 + $numvote;
$newvote2 = $vote2 + $numvote;
$polloption = $_POST['choice'];
if ($polloption == "") {
$error = "You didn't select any option.";
} elseif ($polloption == "one") {
$stmt2 = $db->prepare("UPDATE polls SET vote_one=? WHERE poll_subject=?");
$stmt2->execute(array($newvote1, $subject));
} else {
$stmt2 = $db->prepare("UPDATE polls SET vote_two=? WHERE poll_subject=?");
$stmt2->execute(array($newvote2, $subject));
}
}
}
}