-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 -->
結果:
的請求被執行,我看到一個新的行被添加到我的數據庫,但問題是,我總是得到一個消息,說:「有一個問題:(」至極意味着我的功能不會像它應該的那樣返回true。任何想法爲什麼這不起作用?我很感謝你們的幫助,並且提前感謝你!
如果一切正常或錯誤,如果有問題,它將返回true。你可以檢查Kommentar.php我的函數在哪裏實現。 controller_createKommentar()只是Kommentar.php在我的函數實現的地方和我使用它的View之間的橋樑(爲了尊重mvc模式)。 –
我引用的函數可能會調用返回布爾值的方法,但函數本身不會返回該值。你需要'返回$ GLOBALS ['kommentar'] - > createKommentar();' –
沒錯!非常感謝Tim Morton。你救了我的生命:)信不信由我昨天開始一直在尋找答案。我的天啊 !! –