PHP评论审核系统

时间:2016-06-18 00:28:46

标签: php forms

我在php中编写了一个使用一个包含六行的表的注释系统:

  • id(AI Primary)
  • 状态(等待,已批准或已拒登)
  • 标题(评论主题)
  • 消息(实际评论)
  • 学生(发布者姓名)
  • 日期时间(日期时间)
  • ip(发布的电脑的IP)
  • 发布评论后,状态列会自动设置为等待'。我正在创建一个一次显示一行的页面和两个html按钮,' Approve'和'拒绝',当点击其中一个按钮时,状态栏分别更新为这两个按钮的值。这是我的代码:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "root";
    $dbname = "kevin";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $sql = "SELECT * FROM waiting WHERE status = 'waiting' LIMIT 1";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "
            <p><b>".$row["title"]."</b></p> 
            <p>".$row["message"]."</p>
            <p><i>". $row["student"]."</i></p>
            <p>". $row["datetime"]."&nbsp&nbsp&nbsp&nbsp". $row["ip"]."</p>.
            <form action='' method='POST'>
            <input type='button' name='approve' style='color:green;' value='Approve'>&nbsp&nbsp&nbsp&nbsp
            <input type='button' name='disapprove' style='color:red;' value='Disapprove'>
            </form>
            ";
            $id = $row["id"];
        if(isset($_POST['approve'])){
            $sql = "UPDATE waiting SET status='approve' WHERE id=".$id."";
    
            if ($conn->query($sql) === TRUE) {
                echo "Record updated successfully - Approved";
            } else {
                echo "Error updating record: " . $conn->error;
            }
        }
        if(isset($_POST['disapprove'])){
            $sql = "UPDATE waiting SET status='disapprove' WHERE id=".$id."";
    
            if ($conn->query($sql) === TRUE) {
                echo "Record updated successfully - Disapproved-";
            } else {
                echo "Error updating record: " . $conn->error;
            }
        }
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
    

    最终用户将有另一个页面显示所有评论,其状态栏已阅读&#39;已批准&#39;。

    1 个答案:

    答案 0 :(得分:0)

    首先,从&#34;按钮&#34;更改输入的类型。提交&#34;。

    其次,如果&#34;&#34;&#34;块。

    第三,添加带ID字段的隐藏输入,以查看正在更新的记录。

    <?php
    
    if(isset($_POST['approve'])){
        $sql = "UPDATE waiting SET status='approve' WHERE id=".$_POST['id']."";
    
        if ($conn->query($sql) === TRUE) {
            echo "Record updated successfully - Approved";
        } else {
            echo "Error updating record: " . $conn->error;
        }
    }
    if(isset($_POST['disapprove'])){
        $sql = "UPDATE waiting SET status='disapprove' WHERE id=".$_POST['id']."";
    
        if ($conn->query($sql) === TRUE) {
            echo "Record updated successfully - Disapproved-";
        } else {
            echo "Error updating record: " . $conn->error;
        }
    }
    
    $sql = "SELECT * FROM waiting WHERE status = 'waiting' LIMIT 1";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
            echo "
            <p><b>".$row["title"]."</b></p> 
            <p>".$row["message"]."</p>
            <p><i>". $row["student"]."</i></p>
            <p>". $row["datetime"]."&nbsp;&nbsp;&nbsp;&nbsp;". $row["ip"]."</p>.
            <form action='' method='POST'>
            <input type='submit' name='approve' style='color:green;' value='Approve'>&nbsp;&nbsp;&nbsp;&nbsp;
            <input type='submit' name='disapprove' style='color:red;' value='Disapprove'>
            <input type='hidden' name='id' style='color:red;' value='".$row['id']."'>
            </form>
            ";
            $id = $row["id"];
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>
    } else {
        echo "0 results";
    }
    $conn->close();
    ?>