Why do I get error : Undefined variable?

前端 未结 4 461
难免孤独
难免孤独 2021-01-29 10:25

I created an input script. I write name and script post name into database. But I have error - ErrorException [ Notice ]: Undefined variable: result .

There

4条回答
  •  时光说笑
    2021-01-29 11:06

    Possibly because an empty value was passed to name and the variable doesn't get initialized unless its non-empty. But it gets used in the following line, outside the if

    $this->template->content = View::factory('about/about')->set('result', $result);
    

    Initialize $result outside the if():

    $result = "";
    if(!empty($_POST['name'])){
        $name = Model::factory('index')->insert_names($_POST['name']);;
        $result= $name;
    }
    

    Or move the entire block that follows the if(){} inside it.

    public function action_index()
    {
        if(!empty($_POST['name'])){
          $name = Model::factory('index')->insert_names($_POST['name']);;
          $result= $name;
    
          // move this inside the if()
          $this->template->site_name = Kohana::$config->load('common')->get('site_name');
          $this->template->site_description = Kohana::$config->load('common')->get('site_description');
          $this->template->page_title = 'About';
          $this->template->content = View::factory('about/about')->set('result', $result);
          $this->template->styles[] = 'index/index';
       }
    }
    

提交回复
热议问题