Search code examples
phpmysqlmodel-view-controllerpdonull

Function returning null


I need your help with a PHP problem I'm facing right now. So I did create a function to add a new comment (I'm working on a blog). This latter is not supposed to return a null in case everything run as it's expected. To understand please see my code below.

I'm using MVC so it looks like that:

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

Result:

The Request is executed and I see that a new line is added to my DB but the problem is that I get always a message that "There is a problem" which means my function doesn't return true as it's supposed to be. Any Idea why is that not working? I appreciate your help guys and thank you in advance!


Solution

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

    Your function isn't returning anything.