php表单不使用MVC写入数据库

时间:2014-10-28 18:36:00

标签: php mysql sql oop

我正试图在MVC架构中使用php创建一个博客 我的数据库如下所示:

TABLE `blog_posts` (
    `post_id` INT(11) unsigned NOT NULL AUTO_INCREMENT,
    `user_name` VARCHAR(64),
    `post_title` VARCHAR(255) DEFAULT NULL,
    `post_desc` TEXT,
    `post_cont` TEXT,
    `post_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`post_id`),
 FOREIGN KEY (`user_name`) REFERENCES users (`user_name`) ON DELETE RESTRICT ON UPDATE CASCADE

现在,我正在尝试创建一个博客帖子,我无法弄清楚它为什么不起作用 我只是在我的数据库中获取默认值,而不是我写入表单时的默认值。

这是我的代码

控制器

/**
 * Creates a new blogpost. This is the target of form submit action.
 */
public function create()
{
    if (!empty($_POST['post_title']) AND !empty($_POST['post_desc']) AND !empty($_POST['post_cont']) AND isset($_POST['post_cont'])) {
        $note_model = $this->loadModel('Blog');
        $note_model->create($_POST['post_title'] AND $_POST['post_desc'] AND $_POST['post_cont']);
    }
    header('location: ' . URL . 'blog');
}

模型

    /**
     * Setter for a blogpost (create)
     * @param string $post_cont blogpost text that will be created
     * @return bool feedback (was the post created properly ?)
     */
    public function create($post_title, $post_desc, $post_cont)
    {
        $sql = "INSERT INTO blog_posts (post_title, post_desc, post_cont, user_name) VALUES (:post_title, :post_desc, :post_cont, :user_name)";
        $query = $this->db->prepare($sql);
        $query->execute(array(':post_title' => $post_title, ':post_desc' => $post_desc, ':post_cont' => $post_cont, ':user_name' => $_SESSION['user_name']));

        $count =  $query->rowCount();
        if ($count == 1) {
            $_SESSION["feedback_positive"][] = FEEDBACK_BLOGPOST_CREATION_SUCCESSFUL;
            return true;
        } else {
            $_SESSION["feedback_negative"][] = FEEDBACK_BLOGPOST_CREATION_FAILED;
        }
        // default return
        return false;
    }

查看

<form method="post" action="<?php echo URL;?>blog/create">
        <label>Title: </label><input type="text" name="post_title" />
        <label>Description: </label><input type="text" name="post_desc" />
        <label>Blogpost: </label><textarea name="post_cont"></textarea>
        <input type="submit" value='create blogpost' autocomplete="off" />
    </form>

非常感谢任何形式的帮助!谢谢!!

1 个答案:

答案 0 :(得分:0)

@u_mulder有权。

你必须像这样使用这个电话:

$note_model->create($_POST['post_title'], $_POST['post_desc'], $_POST['post_cont']);