2011-10-07 117 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

相關問題