为什么会出现错误:未定义变量?
我创建了一个输入脚本.我将名称和脚本发布名称写入数据库.但是我有错误-ErrorException [ Notice ]: Undefined variable: result
.
I created an input script. I write name and script post name into database. But I have error - ErrorException [ Notice ]: Undefined variable: result
.
有我的控制器:
class Controller_About extends Controller_Template{
public function action_index()
{
if(!empty($_POST['name'])){
$name = Model::factory('index')->insert_names($_POST['name']);;
$result= $name;
}
$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';
}
}
我的观点是
<form action="">
<input type="text" name="name" />
</form>
这是我的模型:
Class Model_Index Extends Model {
public static function insert_names($name){
$query = DB::query(DATABASE::INSERT, 'INSERT INTO names (name) VALUES (:name)')->parameters(array(':name' => $name));
}
}
问题出在哪里?
编辑#1
我编辑了控制器:
class Controller_About extends Controller_Template{
public function action_index()
{$result = '';
if(!empty($_POST['name'])){
$name = Model::factory('index')->insert_names($_POST['name']);;
$result= $name;
}
$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';
}
}
但是这不起作用,因为当我输入名称时,它们不会放入数据库中.
But this not working, because when I input name, they not puts into database.
可能是因为向name
传递了一个空值,并且除非该变量为非空值,否则不会对其进行初始化.但是它在if
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);
在if()
之外初始化$result
:
$result = "";
if(!empty($_POST['name'])){
$name = Model::factory('index')->insert_names($_POST['name']);;
$result= $name;
}
或将其内跟随if(){}
的整个块移动.
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';
}
}