繁体   English   中英

在CodeIgniter中调用方法时,也会触发另一种方法

[英]A different method is also getting triggered when calling a method in CodeIgniter

调用方法时,也会调用另一个尚未调用的方法。

视图

<form method="post" action="<?php echo 'http://localhost/LinkLab/index.php/Comments/insertcomment/'.$postarray[0]->postID; ?>">
                        <div class="form-group">
                            <input type="text" class="form-control" rows="1" required placeholder="Username" name="username">
                            <br>
                            <textarea class="form-control" rows="3" required placeholder="Comment" name="comment"></textarea>
                        </div>
                        <input type="submit" class="btn btn-info submitbtn" name="submit" value="Submit"/>
</form>

调节器

public function insertcomment($postID){
        $this->load->helper('url');
        $this->load->model('comments_model');
        date_default_timezone_set('Asia/Colombo');

        if ($this->input->post('submit') == true){
            $commentarray = array(
                'postID' => $postID,
                'commentID' => round(microtime(true)*1000),
                'username' => $this->input->post('username'),
                'comment' => $this->input->post('comment'),
                'timestamp' => date("Y-m-d h:i:s"),
            );
            $result = $this->comments_model->inserCommentToDB($commentarray);
        }
        header('Location:http://localhost/LinkLab/index.php/Comments/'.$postID);
}

模型

class Comments_model extends CI_Model {

    function retrievePostFromDB($postID) {
        $sql = "SELECT * FROM post WHERE postID = ".$postID;
        $query = $this->db->query($sql);
        $postarray = $query->result();
        return $postarray;
    }
    function inserCommentToDB($commentarray) {
        print_r($commentarray);
        $this->db->insert('comments', $commentarray);
        if ($this->db->affected_rows() > 0) {
            return true;
        } else {
            return false;
        }
    }
}

在控制器中,当它调用“inserCommentToDB”方法时,看起来像“retrievePostFromDB”方法也被调用了。 它给出了以下错误。

发生数据库错误

错误号码:1054

'where子句'中的未知列'insertcomment'

SELECT * FROM post WHERE postID = insertcomment

文件名:C:/wamp/www/LinkLab/system/database/DB_driver.php

行号:691

造成这种情况的原因是什么? 谢谢。

要获得有关您的问题的帮助,您需要在评论控制器中提供更多信息,如索引功能,如果您使用的是_remap方法,则需要提供路由.php文件的内容

至于提供的代码; 我有一些意见要改进它。

1-如果您的系统使用特定时区; 在index.php文件中设置时区(每次要插入或更新内容时都不需要设置时区)。

2-如果您希望手动编写表单,请使用form_helper构建表单或者至少使用form_open方法,或者只使用site_url链接表单而不是手动将表单连接到localhost; 目前您的脚本需要修改才能上传到webhost。

3-使用form_validation库验证您没有发布空注释; 检查用户是否提交表单对您没有任何好处。

4- CodeIgniter有一个重定向功能,你可以使用它而不是手动使用头功能; 这将允许您使用'controller / method'重定向,这样您就不必将自己锁定到localhost(再次节省您需要上传到webhost的时间)

5-由于您使用的是注释模型,因此您不必在方法中使用“注释”一词; 这是多余的。

6-评论模型与检索帖子无关,你应该从posts_model中检索你的帖子。

7-使用查询构建器来避免SQL注入,它将自动转义您在'where'语句中使用的值。

8-虽然你生成评论ID的方式看起来很好; 通常你应该使用auto_increment id来避免自己计算id。

9-如果在太多位置使用帮助程序或库,则使用autoload.php加载这些程序,以便它们可以在应用程序的所有方法中使用; 至于模型加载通常你在__construct中加载它们,例如,posts控制器中的每个方法都将使用posts_model,因此在__construct中加载它将节省你的时间和精力

最后我认为使用camel-case命名你的SQL列是一个坏习惯; 我更喜欢使用带下划线的所有小写字母来分隔单词,但这只是一个偏好问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM