2017-05-26 7 views
-2

私は、ユーザーがスライダーで選択した気分を投稿できるようにする関数をコーディングしようとしました。私は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'); 
} 

これらは2つの機能です。

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> 
+0

代わりにあなたのボタンがいくつかで提出され – DamiToma

+0

+0

ボタンは 'type'が' submit'を、フォームはgetを、OPは '$ _POST'を使ってデータにアクセスしています。 – julekgwa

答えて

0

代わり$_POSTの使用$_GET、フォームが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'); 
} 
+0

私はあなたのコードを使ってみると、次のように書かれています: '注意:未定義のインデックス:気分はC:\ xampp \ htdocs \ Prototype2 \ mood.php 36行目 注意:未定義インデックス:moodIDはC:\ xampp \ htdocs \ Prototype2 \ mood.php on line 43 ' –

+0

あなたのフォームに' moodID'がないので – julekgwa

関連する問題