在php发布后,投票仍在增加

时间:2014-04-29 02:12:03

标签: php post

我正在使用早期投票系统来比较两组图像。当您单击投票按钮时,它会将数据库增加1。一切正常,问题是如果我第一次点击另一个投票按钮,它会在错误的一个上增加1,然后它就可以了。

我想也许我还有帖子或者什么东西,但是我把未设置的帖子放了,但是没有解决它。 网站在这里http://thereal805productions.com/scratchpad.php 有什么想法吗?

(这是原始尝试)

if ($_POST) {

    $sql = "SELECT * FROM image_vote where id = :id ";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':id', $image_rand);
    $stmt->execute();
    $row = $stmt->fetch();
    $filename_1 = $row['filename_1'];
    $vote_1 = $row['vote_1'];
    $filename_2 = $row['filename_2'];
    $vote_2 = $row['vote_2'];

    if (isset($_POST['left'])) {
        $vote_1++;  
        unset($_POST['left']);          
    } 

    if (isset($_POST['right'])) { 
        $vote_2++; 
        unset($_POST['right']);
    } 

    $sql = "UPDATE image_vote SET vote_1 =?, vote_2 = ? WHERE id = ? ";
    $stmt = $conn->prepare($sql);
    $stmt->execute(array($vote_1, $vote_2, $image_rand));
    $done = $stmt->rowCount();

}

然后根据我尝试的建议:

if (isset($_POST['left'])) {

        $sql = "SELECT * FROM image_vote where id = :id ";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $image_rand);
        $stmt->execute();
        $row = $stmt->fetch();
        $vote_1 = $row['vote_1'];

        $vote_1++;  
        echo "<br>I just voted the left";
        $sql = "UPDATE image_vote SET vote_1 =? WHERE id = ? ";
         $stmt = $conn->prepare($sql);
         $stmt->execute(array($vote_1, $image_rand));
        unset($_POST);  
        // echo "<br> ". var_dump($_POST);  // I get error here $_POST not set      
    } 

if (isset($_POST['right'])) {

        $sql = "SELECT * FROM image_vote where id = :id ";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $image_rand);
        $stmt->execute();
        $row = $stmt->fetch();
        $vote_2 = $row['vote_2'];


        $vote_2++;  
        echo "<br>I just voted the left";
        $sql = "UPDATE image_vote SET vote_2 =? WHERE id = ? ";
         $stmt = $conn->prepare($sql);
         $stmt->execute(array($vote_2, $image_rand));
        unset($_POST);  
        // echo "<br> ". var_dump($_POST);  // I get error here $_POST not set      
    } 

仍然没有运气。

在unset($ _ POST)之后

var_dump($ _ POST)确认是的,该帖子未设置。并且另一篇文章中的echo语句没有触发,只是增量增加......

注意,我尝试一起禁用第二个帖子(比如说右边),但是第一次点击投票时,左边会增加。所以它必须仍然在解雇......

进一步反思......

我认为它毕竟不会增加,我认为这实际上是一个显示问题,当我点击帖子时显示器正在赶上

基本上我认为这句话搞砸了。

    <?php 
    if (isset($_POST['left']) || isset($_POST['right']) ) { 

        $sql = "SELECT * FROM image_vote where id = :id ";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $image_rand);
        $stmt->execute();
        $row = $stmt->fetch();
        $vote_1 = $row['vote_1'];
        $vote_2 = $row['vote_2'];

        ?>

        <tr>
            <td>Vote Total = <?php echo $vote_1 ?> </td>
            <td></td>
            <td>Vote Total = <?php echo $vote_2 ?> </td>
        </tr> 
    <?php } ?>

1 个答案:

答案 0 :(得分:0)

因此,经过进一步研究,我的问题是结构问题。基本上我的投票显示部分与我的投票部分分开,因此当我发布它时,看起来投票再次增加而不是现实,即投票显示只是更新。

正确显示的新代码是

                    if ($_POST) {

                        $sql = "SELECT * FROM image_vote where id = :id ";
                        $stmt = $conn->prepare($sql);
                        $stmt->bindParam(':id', $image_rand);
                        $stmt->execute();
                        $row = $stmt->fetch();
                        $filename_1 = $row['filename_1'];
                        $vote_1 = $row['vote_1'];
                        $filename_2 = $row['filename_2'];
                        $vote_2 = $row['vote_2'];
                        // echo "vote 1 = $vote_1 and vote 2 = $vote_2 <br>";

                        if (isset($_POST['left'])) {
                        $vote_1++;  
                        echo "<br>I just voted the left";
                        $sql = "UPDATE image_vote SET vote_1 =? WHERE id = ? ";
                         $stmt = $conn->prepare($sql);
                         $stmt->execute(array($vote_1, $image_rand));
                        unset($_POST);  
                        // echo "<br> ". var_dump($_POST);  // I get error here $_POST not set      
                        } 

                        if (isset($_POST['right'])) { 
                        $vote_2++; 
                        echo "<br>I just voted the right";
                        $sql = "UPDATE image_vote SET vote_2 =? WHERE id = ? ";
                         $stmt = $conn->prepare($sql);
                         $stmt->execute(array($vote_2, $image_rand));
                        unset($_POST);  
                        // echo "<br> ". var_dump($_POST);  
                        } 


                        $sql = "SELECT * FROM image_vote where id = :id ";
                        $stmt = $conn->prepare($sql);
                        $stmt->bindParam(':id', $image_rand);
                        $stmt->execute();
                        $row = $stmt->fetch();
                        $vote_1 = $row['vote_1'];
                        $vote_2 = $row['vote_2'];
                        ?>
                        <tr>
                            <td>Vote Total = <?php echo $vote_1 ?> </td>
                            <td></td>
                            <td>Vote Total = <?php echo $vote_2 ?> </td>
                        </tr> <?php
                   } 
                        ?>

感谢所有的帮助,非常感谢。