无法将我的投票数更新到我的数据库中。我无法弄清楚原因。
这是代码。我有下拉菜单,文本字段和提交按钮。在下拉菜单中选择一位老师,然后在文本字段中输入分数,然后提交按钮将执行脚本
<form method='post' action='input_stack.php'>
<input type='text' name='votecount'>
<select name='teacher_dropdown'>
<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<option value'" . $row['facultyname'] ."'>" . $row['facultyname'] . "</option>";
}
?>
</select>
<input type='submit' name='submit' value='Submit now'>
</form>
</body>
</html>
<?php mysql_free_result($result);?>
和这个
<?php
$teacherz = isset($_POST['teacher_dropdown']) ? $_POST['teacher_dropdown'] : "";
$votecount = isset($_POST['votecount']) ? $_POST['teacher_dropdown'] : "";
if (isset($_POST['submit'])) {
$sql = "UPDATE subj_eva SET facultyname='$teacherz', totalvotes = totalvotes + '$votecount'";
}
$result=mysql_query($sql);
?>
由于变量,选项值或缺少的WHERE子句,我不确定它是不是。
答案 0 :(得分:1)
您在查询中缺少WHERE
子句。它应该是:
$sql = "UPDATE subj_eva
SET totalvotes = totalvotes + $votecount
WHERE facultyname = '$teacherz'"
您的HTML中也有拼写错误,=
之后您错过了value
:
echo "<option value='" . $row['facultyname'] ."'>" . $row['facultyname'] . "</option>";
^
您要从错误的$votecount
变量设置$_POST
变量,它应该是:
$votecount = isset($_POST['votecount']) ? $_POST['votecount'] : 0;
您还应升级到PDO或mysqli,以便使用预准备语句。否则,您需要使用mysql_real_escape_string
来清理输入。