【问题标题】:php form does not write into database using MVCphp 表单不使用 MVC 写入数据库
【发布时间】:2014-12-24 07:22:18
【问题描述】:

我正在尝试在 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>

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

【问题讨论】:

  • 使用逗号传递函数的参数,而不是AND
  • 检查错误日志。
  • 谢谢!!解决了!!!

标签: php mysql sql oop


【解决方案1】:

@u_mulder 有权。

你必须像这样使用这个调用:

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

【讨论】:

    猜你喜欢
    • 2011-03-19
    • 2011-12-04
    • 2016-01-13
    • 2020-05-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    • 2015-06-15
    相关资源
    最近更新 更多