Reputation: 55
Can't update my votecount into my database. I can't figure out why.
Here is the codes. I have dropdown menu, a textfield and submit button. Pick a teacher in dropdown menu, then put scores in textfield then submit button will execute the script
<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);?>
and this
<?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);
?>
I'm not sure if it's because of the variable, or option value or maybe the missing WHERE clause.
Upvotes: 0
Views: 14
Reputation: 782285
You're missing the WHERE
clause in your query. It should be:
$sql = "UPDATE subj_eva
SET totalvotes = totalvotes + $votecount
WHERE facultyname = '$teacherz'"
You also have a typo in your HTML, you're missing =
after value
:
echo "<option value='" . $row['facultyname'] ."'>" . $row['facultyname'] . "</option>";
^
You're setting the $votecount
variable from the wrong $_POST
variable, it should be:
$votecount = isset($_POST['votecount']) ? $_POST['votecount'] : 0;
You should also upgrade to either PDO or mysqli, so you can use a prepared statement. Otherwise, you need to use mysql_real_escape_string
to sanitize the input.
Upvotes: 1