无法在mysql数据库中更新+1票

时间:2015-02-02 05:11:53

标签: php mysql

我试图找出为什么我无法用我的单选按钮更新我在数据库中的投票。我的编码不是很好,但是我尽力弄明白,但我现在运气不好。

<!doctype html>
<html>
    <head>
    <title>vote3</title>
    </head>

    <body>

<?php


        $users_account = $_GET['users_account'];
        $connect = mysqli_connect('localhost', 'root', 'password','surveytest');
        $query = "SELECT * FROM users_account WHERE username='archievald'";
        $q = mysqli_query($connect, $query);
        while($row = mysqli_fetch_array($q)) {
            $id = $row[0];
            $username = $row[1];
            $password = $row[2];
            $voted = $row[3];
            $acc_type = $row[4];

            echo "<h1>Is he good in english?</h1>";

            ?> 

            <table>
            <form action="" method="POST">

        <?php
            $questions = "SELECT * FROM questions WHERE pollid='test'";
        $q2 = mysqli_query($connect, $questions);
        while($r = mysqli_fetch_array($q2)) {
            $questions = $r[1];
            $votes = $r[2];
            $newvotes = $votes + 1;
            $ip = $_SERVER['REMOTE_ADDR'];
            echo '<tr><td>'.$questions.'</td><td> <input type="radio" name="polloption" value="'.$questions.'" </td></tr>';
            }
            if (isset($_POST['vote'])) {
                $polloption = $_POST['polloption'];
                if ($polloption == "") {
                    die("You didnt select an option.");
                } else {
                    mysqli_query($connect, "UPDATE questions SET votes = $newvotes WHERE questions='$polloption'");
                    die("You voted successfully");
                }
            }
        }
        ?>
        <tr><td><input type="submit" name="vote" value="votes" /></td></tr>
                </form> 
            </table>

    </body>
</html>

据我记得,主要问题是在这里

mysqli_query($connect, "UPDATE questions SET votes = $newvotes WHERE questions='$polloption'");
                        die("You voted successfully");

1 个答案:

答案 0 :(得分:1)

变化

mysqli_query($connect, "UPDATE questions SET votes = $newvotes WHERE questions='$polloption'");
die("You voted successfully");

$con=mysqli_query($connect, "UPDATE questions SET votes = $newvotes WHERE questions='$polloption'");
if(!$con){
    die("Update Query failed");
}else{
    print("You voted successfully.");
}

您对die的使用在执行后抛出错误,这不好,您可以在以下Stack Overflow链接中阅读有关die用法的更多信息:mysqli or die, does it have to die?

希望这有帮助。