-3

I'm trying like 2 hours by now to figure out why my table won't update, and for the love of god, I can't figure it out. It's a simple poll setup. I've replace "UPDATE polls" with "UPDATE comments" and it updates comments without problems. Polls however just won't work so I guess the problem is in the database, but that's as far as I can get. Perhaps someone can help me out with this?

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'");
            }
        }
    }
}

And here is the form code:

<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>
samlev
  • 5,852
  • 1
  • 26
  • 38
S JET
  • 1
  • 1
    you know that after call to `mysqli_query` you need to check whether it was successful and display errors if any, right? – Iłya Bursov Nov 13 '15 at 20:25
  • 1
    Have you tried [`mysqli_commit()`](http://php.net/manual/en/mysqli.commit.php)? – samlev Nov 13 '15 at 20:25
  • @Lashane how do I check weather it was successful? Sorry, I'm into php since for like 2 weeks. Very new to all of this. – S JET Nov 13 '15 at 20:28
  • 1
    You're also mixing `mysql_*` and `mysqli_*`; that's not going to work unless you have opened a database connection for each. Which I guess you haven't as you are missing arguments for your `mysqli_*` functions... – jeroen Nov 13 '15 at 20:29
  • @SJET its very easy, start reading manual http://php.net/manual/en/mysqli.query.php – Iłya Bursov Nov 13 '15 at 20:31
  • @jeroen Jeroen, that solved the problem. Thank you very much! – S JET Nov 13 '15 at 20:32

1 Answers1

0

As mentioned in the comment by @jeroen above, you are mixing up your database APIs. But, what you should really be doing is tossing out both of them and using a higher level API like PDO.

The mysql_* functions have been unsupported for 5 years now, and will be removed entirely from the next version of PHP. The mysqli_* functions are fine if used properly, but PDO provides a number of convenience methods and also allows you to change the underlying database much more easily.

Here is your code fragment quickly rewritten with PDO, using prepared statements. Totally untested but should work, assuming $db is set up correctly. If you plan to ever do anything with a database, you should read up on prepared statements and use them constantly.

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));
            }
        }
    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154