简体   繁体   English

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

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

When calling a method, a different method which hasn't been called is also getting called. 调用方法时,也会调用另一个尚未调用的方法。

View 视图

<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>

Controller 调节器

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);
}

Model 模型

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;
        }
    }
}

In the controller, when it calls the "inserCommentToDB" method, it seems like the "retrievePostFromDB" method is also getting called as it seems. 在控制器中,当它调用“inserCommentToDB”方法时,看起来像“retrievePostFromDB”方法也被调用了。 It gives out the following error. 它给出了以下错误。

A Database Error Occurred 发生数据库错误

Error Number: 1054 错误号码:1054

Unknown column 'insertcomment' in 'where clause' 'where子句'中的未知列'insertcomment'

SELECT * FROM post WHERE postID = insertcomment SELECT * FROM post WHERE postID = insertcomment

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

Line Number: 691 行号:691

What is causing this and what would be the fix? 造成这种情况的原因是什么? Thanks. 谢谢。

To get help regarding your problem you'll need to provide more information like index function in your Comments controller & if you are using _remap method or not + the contents of your route.php file 要获得有关您的问题的帮助,您需要在评论控制器中提供更多信息,如索引功能,如果您使用的是_remap方法,则需要提供路由.php文件的内容

As for the provided code; 至于提供的代码; I have a few comments to improve it. 我有一些意见要改进它。

1- If your system is using a specific time-zone; 1-如果您的系统使用特定时区; set the time-zone in your index.php file (you do not need to set the time-zone each time you want to insert or update things). 在index.php文件中设置时区(每次要插入或更新内容时都不需要设置时区)。

2- Use form_helper to build your form or at least just use the form_open method if you prefer to write your forms manually, or you could just use site_url to link your form instead of manually wiring your form to localhost; 2-如果您希望手动编写表单,请使用form_helper构建表单或者至少使用form_open方法,或者只使用site_url链接表单而不是手动将表单连接到localhost; currently your script requires modifications in-order to upload to a webhost. 目前您的脚本需要修改才能上传到webhost。

3- Use form_validation library to validate you're not posting an empty comment; 3-使用form_validation库验证您没有发布空注释; checking if user is submitting a form isn't doing you any good. 检查用户是否提交表单对您没有任何好处。

4- CodeIgniter has a redirect function which you can use instead of manually using header function; 4- CodeIgniter有一个重定向功能,你可以使用它而不是手动使用头功能; This would allow you to redirect using 'controller/method' so you don't have to lock yourself to localhost (Again save you time when you need to upload to a webhost) 这将允许您使用'controller / method'重定向,这样您就不必将自己锁定到localhost(再次节省您需要上传到webhost的时间)

5- Since you're using comments model, you don't have to use the word 'comment' in your methods; 5-由于您使用的是注释模型,因此您不必在方法中使用“注释”一词; it's redundant. 这是多余的。

6- Comments model has nothing to do with retrieving a post, You should retrieve your posts from a posts_model. 6-评论模型与检索帖子无关,你应该从posts_model中检索你的帖子。

7- Use query builder to avoid SQL injections, it will automatically escape the values you use in your 'where' statement. 7-使用查询构建器来避免SQL注入,它将自动转义您在'where'语句中使用的值。

8- Although the way you're generating comment ids appears to be fine; 8-虽然你生成评论ID的方式看起来很好; Normally you should be using an auto_increment id to avoid having to calculate an id yourself. 通常你应该使用auto_increment id来避免自己计算id。

9- If you use a helper or library in too many locations then use autoload.php to load these so they become available across all methods in your application; 9-如果在太多位置使用帮助程序或库,则使用autoload.php加载这些程序,以便它们可以在应用程序的所有方法中使用; As for model loading normally you load these in your __construct for example every single method in a posts controller will be using posts_model therefore having it loaded in __construct will save your time & effort 至于模型加载通常你在__construct中加载它们,例如,posts控制器中的每个方法都将使用posts_model,因此在__construct中加载它将节省你的时间和精力

Finally I think using camel-case to name your SQL columns is a bad habit; 最后我认为使用camel-case命名你的SQL列是一个坏习惯; I prefer using all lower-case with underscores to separate words, but this is just a matter of preference. 我更喜欢使用带下划线的所有小写字母来分隔单词,但这只是一个偏好问题。

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

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