如何输出双入口PHP MVC的错误

时间:2014-05-27 02:55:07

标签: php sql codeigniter duplication

我正在使用Codeigniter,我创建了一个代码,用于检查数据库中是否已存在相同的条目。但我不知道我将如何输出错误信息。布尔值不起作用。

查看

<h8><b>Add New Service: For single upload. <?php echo $status; ?></b></h8><hr>
                        <form action="<?php echo base_url(); ?>some_controller/insertServ" method="post">
                                    <center>Service Name: <input type="text"  name="ci_name"/>
                                    <input type="submit" class="classname" value="Save"/></center>
                        </form>

CONTROLLER

public function insertServ(){
    /* logic behind adding a new service
     */

        $ci_name = $this->input->post('ci_name');

        $success = $this->some_model->addCI($ci_name);

        if($success == TRUE)
            $this->viewMap_add(TRUE);
        else $this->viewMap_add(FALSE);


}

public function viewMap_add($success = NULL){
    /* Shows the list of the services with a dialog box for 
     * adding a new service
     */
     if($success == NULL)
        $status = 'N/A';
    else if($success == TRUE)
        $status = 'Success';
    else $status =  'FAILED';

    $data['status'] = $status;
        $data['current_user']=$this->session->userdata('email');
        $data['mapList'] = $this->some_model->getMapped();
        $this->load->view('templates/header.php',$data);
        $this->load->view('some_page/servList_add.php',$data);

}

MODEL

public function addCI($ci_name){
    /* Adds a new service
     */
    $ci_name = $this->db->escape_str($ci_name);

    $queryStr = "Select service from appwarehouse.service where service = '$ci_name'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){
       echo "result already exists";
     }
     else{
    $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name');"; 
    $query = $this->db->query($queryStr);}

}

1 个答案:

答案 0 :(得分:0)

首先,您的模型方法addCI不会返回任何内容。解决了这个问题。

然后,您可以通过简单地删除一层代码并直接发送状态值来避免所有混乱:

公共职能insertServ

if($success)
    $this->viewMap_add('Success');
else 
    $this->viewMap_add('FAILED');

然后只需删除viewMap_add方法中的所有if-elses。

此外,由于您没有告诉确切是什么问题,请尝试对insertServ中的状态变量执行var_dump,或者只是:

var_dump($this->some_model->addCI($ci_name));

更新

以下是您的模型方法应该如何(没有回应那里,但返回一个布尔值):

public function addCI($ci_name){
    /* Adds a new service
     */
    $ci_name = $this->db->escape_str($ci_name);

    $queryStr = "Select service from appwarehouse.service where service = '$ci_name'";
    $query = $this->db->query($queryStr);
    if($query->num_rows()>0){
        return false; // <---- this one is important
    }
    else{
        $queryStr = "INSERT INTO appwarehouse.service(service) VALUES ('$ci_name');"; 
        $query = $this->db->query($queryStr);
        return true; // <---- and this one
    }
}