Jump to content

Basic MVC Issue


johnnys
Go to solution Solved by 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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.