我的功能没有在数据库中发布

时间:2017-05-26 18:34:40

标签: php

所以我尝试编写一个允许用户发布由滑块选择的情绪的函数。我成功地将颜色与ID连接,然后我尝试制作帖子功能。然而,没有任何事情发生。

这是我称之为功能的地方。

if (isset($_POST['ready'])) {
    $mood = new Post();
    $moodColor = $_POST['mood'];
    $statementMood = $mood->getMood($moodColor); //connects the color with an ID

    while ($row = $statementMood->fetch(PDO::FETCH_ASSOC)) {
        $moodID = $row['moodID'];

    }
    $moodID = $_GET['moodID'];
    $userID = $currentUser['userID'];
    $statementPost = $mood->postMood(); //put the emotion in the database.
//header('location: home.php');
}

这是两个功能。

 public function getMood($moodColor){
    $conn = db::getInstance();

    $statementMood = $conn->prepare("SELECT * FROM moods WHERE color = :cMood");
    $statementMood->bindParam(":cMood", $moodColor);
    $statementMood->execute();
    return $statementMood;
}

public function postMood(){
    $conn = db::getInstance();

    $statementPost = $conn->prepare("INSERT INTO postsmoodi (userID, moodID) VALUES (:userID, :moodID)");
    $statementPost ->bindValue(':userID', $this->userID);
    $statementPost->bindValue(':moodID', $this->moodID);
    return $statementPost->execute();
}

这是发布按钮的形式。

<form class="input" action="mood.php" method="get">
    <input id="hiddenValue" type="hidden" class="data" name="mood" value="">
    <button class="moodReady" type="submit" name="ready">Ready</button>
</form>

1 个答案:

答案 0 :(得分:0)

使用$_GET代替$_POST,因为您的表单使用method="get"

if (isset($_GET['ready'])) {
    $mood = new Post();
    $moodColor = $_POST['mood'];
    $statementMood = $mood->getMood($moodColor); //connects the color with an ID

    while ($row = $statementMood->fetch(PDO::FETCH_ASSOC)) {
        $moodID = $row['moodID'];

    }
    $moodID = $_GET['moodID'];
    $userID = $currentUser['userID'];
    $statementPost = $mood->postMood(); //put the emotion in the database.
//header('location: home.php');
}