2011-10-07 123 views
-1

我创建了一个输入脚本。我将名字和脚本帖子名称写入数据库。但我有错误 - 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'; 
    } 
} 

但这不工作,因为当我输入名字,他们没有付诸数据库。

回答

2

可能是因为一个空值传递给name并且该变量没有被初始化,除非它非空。但它可以让在下面的行中使用时,if

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

初始化$resultif()外:

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

或移动如下里面的if(){}整个块。

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'; 
    } 
} 
+0

谢谢!但是这个脚本不起作用,因为当我提交一个名字时,脚本不会把名字放入数据库中。 – reGative

0

您没有POST变量,称为name,因此$result从不设置。

1

方法属性添加到您的窗体:

<form action="" method="post"> 

变化:

if(!empty($_POST['name'])){ 

要:

$result = ''; 
if(!empty($_POST['name'])){ 

,并确保:

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

将在$result为空时工作。

0

你忘了实际运行查询:

public static function insert_names($name) 
{ 
    $query = DB::query(DATABASE::INSERT, 'INSERT INTO names (name) VALUES (:name)')->parameters(array(':name' => $name))->execute(); 
} 

但是它会是一个更好的方法来使用Kohana中的查询生成器:

public static function insert_names($name) 
{ 
    $query = DB::insert('names', array('name'))->values(array($name))->execute(); 
} 

,从你的代码,我考虑可以判断你是初学者,可以直接在控制器中使用ORM并进一步简化它:

if(!empty($_POST['name'])) 
{ 
    $result = ORM::Factory('index')->set(array('name' => $_POST['name']))->save(); 
} 

但是,问题仍然存在,因为您的insert_names方法不会返回任何内容,所以您会将模板的结果变量设置为FALSE。

我相信你会想要做的是这样的:

public static function insert_names($name) 
{ 
    if(DB::insert('names', array('name'))->values(array($name))->execute()) 
    { 
     return $name; 
    } 
} 

(ORM与它不会是必要创建摆在首位此方法)

我看到了另一个错误你的控制器虽然 - 我想你不习惯E_NOTICE错误。而不是设置$结果为空字符串,它最好能够简单地重构你的代码一点点:

if(!empty($_POST['name'])) 
{ 
    $this->template->content = View::factory('about/about'); 

    if($name = Model::factory('index')->insert_names($_POST['name'])) 
    { 
     $this->template->content->set('result', $_POST['name']); 
    } 
    else 
    { 
     // some kind of error message 
    } 
} 

这可能是一个好主意,一群来自模板所有这些变量为一体,幸福的家庭:

class Controller_About extends Controller_Template{ 
    public function action_index() 
    { 
     $config = Kohana::$config->load('common'); 
     $this->template->set(array(
      'site_name' => $config->get('site_name'), 
      'site_description' => $config->get('site_description'), 
      'page_title' => 'About', 
      'styles' => 'index/index' 
     )); 

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

     if($name = Model::factory('index')->insert_names($_POST['name'])) 
     { 
      $this->template->content->set('result', $_POST['name']); 
     } 
     else 
     { 
      // some kind of error message 
     } 
    } 
} 

那里。是不是A LOT更清洁? :)

虽然它仍然可以使用验证,但是这并不包括你的原始问题,所以我只是留下它的方式。

+0

不应该在一个类中使用超级全局变量... –

+0

@MikePurcell。我只重构了作者提供的代码,我不想重写他的*整个应用*。 – d4rky

相关问题