Jump to content

Basic MVC Issue


johnnys

Recommended Posts

I am quite new to php and the mvc setup, I am developing a library app however starting at the very basics so as not to become overwhelmed!

 

I am trying to do a basic insert to my book table, this is the code I have so far alsong with the error I am presented with.

 

Model (models > adminarea_model.php)

 

adminarea_model.php

 

public function create($title_text)
    {
        $title_text = strip_tags($title_text);


        $sql = "INSERT INTO book (title) VALUES (:title)";
        $query = $this->db->prepare($sql);
        $query->execute(array(':title' => $title_text));


        $count =  $query->rowCount();
        if ($count == 1) {
            return true;
        } else {
            $_SESSION["feedback_negative"][] = FEEDBACK_NOTE_CREATION_FAILED;
        }
        return false;
    }

 

View (views > admin > addBook.php)

 

addBook.php

 

<form method="post" action="<?php echo URL;?>admin/create">
<label>Text of new note: </label><input type="text" name="title" />
<input type="submit" value='Create this note' autocomplete="off" />
</form>
 
Controller (controllers > admin.php)
 
admin.php
 
public function create()
    {


        if (isset($_POST['title']) AND !empty($_POST['title'])) {
            $book_model = $this->loadModel('Admin');
            $book_model->create($_POST['title']);
        }
        header('location: ' . URL . 'admin/addBook');
    }
When I am on admin/addBook and I try to submit the form I receive the following error;
Fatal error: Call to a member function create() on a non-object in C:\xampp\htdocs\logintest\application\controllers\admin.php on line 43
Any ideas where I am going wrong?
 
Line 43 contains the following
 
$book_model->create($_POST['title']);
 
Thanks 
 
J
 
Link to comment
https://forums.phpfreaks.com/topic/293038-basic-mvc-issue/
Share on other sites

Probably because that's what you're trying to load:

$book_model = $this->loadModel('Admin');

If admin_model.php doesn't exist, it can't be loaded, so when you try to use $book_model on the next line (the var that you loaded the model into) it's "not an object".

Link to comment
https://forums.phpfreaks.com/topic/293038-basic-mvc-issue/#findComment-1499329
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.