2017-06-22 15 views
-1

私は今直面しているPHPの問題であなたの助けが必要です。だから、私は新しいコメントを追加する関数を作成しました(私はブログで働いています)。この後者は、すべてが期待どおりに実行される場合にnullを返すはずではありません。理解するには下のコードをご覧ください。nullを返す関数

それはそのように見えるので、私はMVCを使用しています:

Kommentar.php

public function createKommentar() { 
    $this->bestaetigung = '0'; 
    $this->istGesehen = '0'; 

    $query = "INSERT INTO " . $this->table_name . " SET BESTAETIGUNG = ?,ISTGESEHEN = ?,INHALT = ?" 
      . ",BENUTZERNAME = ?,IDARTIKEL = ?"; 

    $stmt = $this->conn->prepare($query); 

    $stmt->bindParam(1, $this->bestaetigung); 
    $stmt->bindParam(2, $this->istGesehen); 
    $stmt->bindParam(3, $this->kommentarInhalt); 
    $stmt->bindParam(4, $this->benutzerName); 
    $stmt->bindParam(5, $this->idArtikel); 

    $stmt->execute(); 
    return $stmt; 
} 

Controller_kommentar.php:

include_once '/../Model/objects/Kommentar.php'; 
include_once 'controller_bd.php'; 
$db = controller_connection(); 
$GLOBALS['kommentar'] = new Kommentar($db); 
function controller_createKommentar() { 
    $GLOBALS['kommentar']->createKommentar(); 
} 

はView.php

<?php 
include_once '../../../Controller/controller_bd.php'; 
include_once '../../../Controller/controller_kommentar.php'; 

if ($_POST) { 
if (!empty($_POST['benutzerName']) && !empty($_POST['kommentarInhalt'])) { 
    controller_setBenutzerName($_POST['benutzerName']); 
    controller_setKommentarInhalt($_POST['kommentarInhalt']); 
    controller_setIdArtikel(100); 
    if (controller_createKommentar()) { 
     echo 'everything's ok!'; 
    } else { 
     echo 'There is a problem :('; 
     } 
    } 
} 
?> 

div class="row mt"> 
      <div class="col-lg-12"> 
       <div class="form-panel"> 
         <form action="TEST.php" class="form-horizontal style-form" method="post"> 
         <div class="form-group"> 
          <label class="col-sm-2 col-sm-2 control-label">benutzerName</label> 
          <div class="col-sm-10"> 
           <input type="text" name="benutzerName" class="form-control"> 
          </div> 
         </div> 
         <div class="form-group"> 
          <label class="col-sm-2 col-sm-2 control-label">kommentarInhalt</label> 
          <div class="col-sm-10"> 
           <input type="text" name="kommentarInhalt" class="form-control"> 
          </div> 
         </div> 
         <div class="form-group"> 
           <button type="submit">submit</button> 
         </div> 

        </form> 
       </div> 
      </div><!-- col-lg-12-->   
     </div><!-- /row --> 

結果:

リクエストが実行され、私は新しい行が私のDBに追加されたことがわかりますが、問題は、私はウィッヒ「問題は:(ある」ことを常にメッセージを取得することです私の関数は、それが仮定されているように真を返さないことを意味します。どんなアイデアでも動作しないのはなぜですか?私はあなたの助けの人に感謝し、事前に感謝!

答えて

0
function controller_createKommentar() { 
    $GLOBALS['kommentar']->createKommentar(); 
}  

あなたの関数は何も返しません。

+0

問題がある場合はすべてがOKまたはFalseの場合はtrueを返します。私の機能が実装されているKommentar.phpを確認することができます。 controller_createKommentar()は、私の関数が実装されているKommentar.phpと、それを使用しているView(mvcパターンを考慮して)の間のブリッジです。 –

+0

私が引用した関数は、ブール値を返すメソッドを呼び出すかもしれませんが、関数自体はその値を返しません。あなたは$ GLOBALS ['kommentar'] - > createKommentar();を返す必要があります。 –

+0

そうですよ!そんなにTim Mortonさんありがとうございました。あなたは私の人生を救った:それを信じるかどうか私は昨日から答えを探していた。ああ、神様 !! –

関連する問題