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

在php中发布后,投票仍在增加,php,post,Php,Post,我正在研究一个提前投票系统来比较两组图像。单击“投票”按钮时,数据库的增量为1。这一切都很好,问题是,如果我第一次点击另一个投票按钮,它会在错误的按钮上增加1,然后它就会正常工作 我想也许我还留着那根柱子什么的,但我把它放在了未固定的柱子上,这并没有解决它。 网站在这里 有什么想法吗 (这是最初的尝试) 然后根据我尝试的建议: if (isset($_POST['left'])) { $sql = "SELECT * FROM image_vote where id = :id

我正在研究一个提前投票系统来比较两组图像。单击“投票”按钮时,数据库的增量为1。这一切都很好,问题是,如果我第一次点击另一个投票按钮,它会在错误的按钮上增加1,然后它就会正常工作

我想也许我还留着那根柱子什么的,但我把它放在了未固定的柱子上,这并没有解决它。 网站在这里 有什么想法吗

(这是最初的尝试)

然后根据我尝试的建议:

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      
    } 
if(isset($\u POST['left'])){
$sql=“从图像中选择*,其中id=:id”;
$stmt=$conn->prepare($sql);
$stmt->bindParam(':id',$image\u rand);
$stmt->execute();
$row=$stmt->fetch();
$vote_1=$row['vote_1'];
$vote_1++;
回声“
我刚刚投了左派的票”; $sql=“UPDATE image\u vote SET vote\u 1=?其中id=?”; $stmt=$conn->prepare($sql); $stmt->execute(数组($vote\u 1,$image\u rand)); 取消设置(美元邮政); //echo“
”.var\u dump($\u POST);//我在这里得到错误$\u POST未设置 } 如果(isset($_POST['right'])){ $sql=“从图像中选择*,其中id=:id”; $stmt=$conn->prepare($sql); $stmt->bindParam(':id',$image\u rand); $stmt->execute(); $row=$stmt->fetch(); $vote_2=$row['vote_2']; $vote_2++; 回声“
我刚刚投了左派的票”; $sql=“更新图像投票集投票投票2=?其中id=?”; $stmt=$conn->prepare($sql); $stmt->execute(数组($vote_2,$image_rand)); 取消设置(美元邮政); //echo“
”.var\u dump($\u POST);//我在这里得到错误$\u POST未设置 }
还是不走运

未设置($\u POST)后的var_dump($\u POST)确认POST是未设置的。另一篇文章中的echo语句没有触发,只是增量增加了

请注意,我曾尝试禁用第二个帖子(比如右边的帖子),但在我第一次点击vote时,左边的帖子会增加。所以它一定还在发射

经过进一步思考

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

基本上我认为这句话把事情搞砸了

    <?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 } ?>

投票总数=
投票总数=

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

正确显示的新代码为

                    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
                   } 
                        ?>
if($\u POST){
$sql=“从图像中选择*,其中id=:id”;
$stmt=$conn->prepare($sql);
$stmt->bindParam(':id',$image\u 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'];
//回声“投票1=$vote_1和投票2=$vote_2
”; 如果(isset($_POST['left'])){ $vote_1++; 回声“
我刚刚投了左派的票”; $sql=“UPDATE image\u vote SET vote\u 1=?其中id=?”; $stmt=$conn->prepare($sql); $stmt->execute(数组($vote\u 1,$image\u rand)); 取消设置(美元邮政); //echo“
”.var\u dump($\u POST);//我在这里得到错误$\u POST未设置 } 如果(isset($_POST['right']){ $vote_2++; echo“
我刚刚投了右票”; $sql=“更新图像投票集投票投票2=?其中id=?”; $stmt=$conn->prepare($sql); $stmt->execute(数组($vote_2,$image_rand)); 取消设置(美元邮政); //echo“
”.var\u dump($\u POST); } $sql=“从图像中选择*,其中id=:id”; $stmt=$conn->prepare($sql); $stmt->bindParam(':id',$image\u rand); $stmt->execute(); $row=$stmt->fetch(); $vote_1=$row['vote_1']; $vote_2=$row['vote_2']; ?> 投票总数= 投票总数=

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

如果你
var\u dump($\u POST)
,你会看到什么?我建议你做两个独立的查询,并在
if(isset($\u POST['left']){
if(isset($\u POST['right'])中放置一个更新{
。按照您现在的方式,它正在更新并设置两个具有相同
id
的列。甚至可以为查询中的列添加一个
+1
。http是无状态的,因此您的_postvar不会被以前的请求污染“http是无状态的,因此您的_postvar不能被以前的请求污染”我不明白这句话的意思,这意味着您尝试“取消设置”POST数据是毫无意义的,因为每个请求都有自己的参数集,而认为前一个请求的POST值可能会对当前请求产生任何影响的想法是完全错误的。
                    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
                   } 
                        ?>