未定义的变量:将字段添加到db中时出错

时间:2017-08-27 21:34:17

标签: php

我收到error:Undefined variable: error 在我的代码中:

            public function add(){
    $this->polls_model->rules = Pf::event()->trigger("filter","polls-adding-validation-rule",$this->polls_model->rules);

    $template = null;
    $template = Pf::event()->trigger("filter","polls-add-template",$template);
    if ($this->request->is_post()){
        $data = array();
        $data["polls_question"] = $this->post->{"polls_question"};
        $data["polls_pubdate"] = str_to_mysqldate($this->post->{"polls_pubdate"},$this->polls_model->elements_value["polls_pubdate"],"Y-m-d H:i:s");
        $data["polls_unpubdate"] = str_to_mysqldate($this->post->{"polls_unpubdate"},$this->polls_model->elements_value["polls_unpubdate"],"Y-m-d H:i:s");
        if (is_array($this->post->{"polls_status"})){
            $data["polls_status"] = implode(",",$this->post->{"polls_status"});
        }else{
            $data["polls_status"] = $this->post->{"polls_status"};
        }
        $port_answer = isset($this->post->{"answer"}) ? $this->post->{"answer"} : array();
        $data = Pf::event()->trigger("filter","polls-post-data",$data);
        $data = Pf::event()->trigger("filter","polls-adding-post-data",$data);
        $var = array();
        $pollq_multiple_yes = intval($this->post->{'pollq_multiple_yes'});
        $data['polls_multiple'] = 0;
        if ($pollq_multiple_yes == 1) {
            if(intval($this->post->{'pollq_multiple'}) > count($port_answer)){
                $data['polls_multiple'] = 1;
            }else{
                $data['polls_multiple'] = intval($this->post->{'pollq_multiple'});
            }
        } else {
            $data['polls_multiple'] = 1;
        }
        //debug($data);
        Pf::database()->query('START TRANSACTION');
        $inserted = $this->polls_model->insert($data);
        if($inserted === false){
            Pf::database()->query('ROLLBACK');
        }else{
            $new_id = $this->polls_model->insert_id();
            $insert_meta = true;
            if(count($port_answer) > 0){
                $custom = array();
                $int = count($port_answer);
                for ($i = 0; $i < $int ; $i++) {
                    if(!empty($port_answer[$i])){
                        $custom = array(
                                'pollsa_qid' => $new_id,
                                'pollsa_answers' => e($port_answer[$i]),
                        );
                    }
                    $insert_meta = $this->answers_model->insert($custom);
                }
                if($insert_meta === false){
                    Pf::database()->query('ROLLBACK');

                }else{
                    Pf::database()->query('COMMIT');
                }
            }
            Pf::database()->query('COMMIT');
        }
        $errors = Pf::validator()->get_readable_errors(false);
        foreach ($errors as $key => $value) {
            $error[$key][0] = $errors[$key][0];
        }

        $this->view->errors =  $error; // error here!
        $var['content'] = $this->view->fetch($template);
        if (count($error) > 0){// and here!!!
            $var['error'] = 1;
        }else{
            Pf::event()->trigger("action","polls-add-successfully",$this->polls_model->insert_id(),$data);
            $var['error'] = 0;
            $var['url'] = admin_url($this->action.'=index&ajax=&id=&token=');
        }

        echo json_encode($var);
    }else{

        $this->view->render($template);
    }
}

我编辑了代码,添加了功能代码。

这是我的添加功能,如果我想添加带有答案的民意调查。

它在我的日志中给了我这个错误。

我找到了这个教程Undefined Variable error in View

我用谷歌搜索了它,但没有发现什么特别的东西可以帮助我。

1 个答案:

答案 0 :(得分:0)

将变量作为数组启动。    因为如果$ error为空,编译器会将其视为数组。   如果没有,它将会出错。

    $error = [];

    $errors = Pf::validator()->get_readable_errors(false);
                 foreach ($errors as $key => $value) {
                   //  $error[$key][0] = $errors[$key][0];
                   // the right way is below
                   // i actually dont know what you want to do but this is the right way         
                   // but providing [0] will make it some how constant.
                   $error[$key] = $errors[$key]
                 }
                $this->view->errors =  $error; // error showing here!
                $var['content'] = $this->view->fetch($template);
                if (count($error) > 0){ // and here???
                    $var['error'] = 1;
                }else{
                    Pf::event()->trigger("action","polls-add-successfully",$this->polls_model->insert_id(),$data);
                    $var['error'] = 0;
                    $var['url'] = admin_url($this->action.'=index&ajax=&id=&token=');
                }