当我单击提交按钮时,为什么isset($ _ POST ['like'])不起作用?

时间:2019-09-28 05:05:29

标签: php

我的POST方法在此代码中不起作用,但是如果将其更改为GET方法,则可以正常工作。

这是我的代码。如果将其更改为GET,那么效果很好。即使使用POST,它也会显示数据库中的赞,但是当我单击“赞”按钮时,它并没有添加更多的赞。

<form action="" method="POST">
    <input type="hidden" name="blogID" value=<?php echo $blogID;?> />
    <input id="likeButton" <?php echo isset($_POST["like"]) ? "disabled" : "";?> type = "submit" value = "Click to Like this Article" name="like"/> <br><br>
    </form>      
    <br>    

    <?php 

    $pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';

    if($pageWasRefreshed )
    {
        $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
            $likes = $conn->query($likesQuery); 
            if ($likes->num_rows > 0) {
                // output data of each row
                while($row2 = $likes->fetch_assoc()) {
                    echo $row2["likes"] . " Likes <br> <br>";
                }
            } else {
                echo "0 Likes";
            }
    }

    else{
        if(isset($_POST['like'])) {
            $blogID=$_POST['blogID'];
            $likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
            $conn->query($likesQuery2);
            $likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID"; 
            $likes3 = $conn->query($likesQuery3); 

            if ($likes3->num_rows > 0) {
                // output data of each row
                while($row = $likes3->fetch_assoc()) {
                    echo $row["likes"] . " Likes <br> <br>";
                }
            } else {
                echo "0 Likes";
            }
        }
        else{
            $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
            $likes = $conn->query($likesQuery); 
            if ($likes->num_rows > 0) {
                // outputz data of each row
                while($row2 = $likes->fetch_assoc()) {
                    echo $row2["likes"] . " Likes <br> <br>";
                }
            } else {
                echo "0 Likes";
            }
        }
    }   
    ;?>

2 个答案:

答案 0 :(得分:0)

在其他部分,您试图以不存在的形式检查isset($_POST['like'])字段。 更新

else{
        if(isset($_POST['like'])) {
            $blogID=$_POST['blogID'];

收件人

else{ 
        if(isset($_POST['blogID'])) {
            $blogID=$_POST['blogID'];

答案 1 :(得分:-1)

好的,问题不在于isset本身,而是$ pageWasRefreshed始终为true,因此无法达到您的$ _POST条件

尝试将您的$ _POST条件放在首位,我认为它将很好地工作

$pageWasRefreshed = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
if(isset($_POST['like'])) {
    $blogID=$_POST['blogID'];
    $likesQuery2 = "UPDATE blogstatus set likes = likes+1 where blogID=$blogID";
    $conn->query($likesQuery2);
    $likesQuery3 = "SELECT likes FROM blogstatus where blogID=$blogID"; 
    $likes3 = $conn->query($likesQuery3); 

    if ($likes3->num_rows > 0) {
        // output data of each row
        while($row = $likes3->fetch_assoc()) {
            echo $row["likes"] . " Likes <br> <br>";
        }
    } else {
        echo "0 Likes";
    }
} else if($pageWasRefreshed ){
    $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
    $likes = $conn->query($likesQuery); 
    if ($likes->num_rows > 0) {
        // output data of each row
        while($row2 = $likes->fetch_assoc()) {
            echo $row2["likes"] . " Likes <br> <br>";
        }
    } else {
        echo "0 Likes";
    }
}
else {
    $likesQuery = "SELECT likes FROM blogstatus where blogID=$blogID"; 
    $likes = $conn->query($likesQuery); 
    if ($likes->num_rows > 0) {
        // outputz data of each row
        while($row2 = $likes->fetch_assoc()) {
            echo $row2["likes"] . " Likes <br> <br>";
        }
    } else {
        echo "0 Likes";
    }
}