2013-02-14 42 views
1

大家好我是Codeigniter的新手,我試圖獲取與從上一頁傳遞的account_id有關的所有數據。PHP使用Codeigniter中的get_where子句不傳遞數據

我傳遞了account_id但沒有傳遞與account_id關聯的名稱字段。名稱字段爲空。

我得到一個錯誤:

這裏是我的控制器代碼:

function input($account_id = '', $name = ''){ 
    if((int)$account_id > 0){ 
     $query = $this->db->select('name', $name); 
     $query = $this->db->get_where('db_accounts', array('account_id' => $account_id)); 

     $data['account'] = $query; 
     $data['fname']['value'] = $name; 
     $data['faccount_id']['value'] = $account_id; 
     $data['name']  = ''; 
     $data['account_id'] = ''; 
    } 

    $this->load->view('manage/input',$data);  
} 

這裏是我的輸入視圖形式:

<?php 

    $data = array(
      'name' => $fname['value'], 
      'account_id' => $faccount_id['value'] 
     ); 

echo '<form action="/manage/edit" method="post" accept-charset="utf-8">'; 
echo form_hidden($data); 

echo $account_id .' Account ID'. 
    form_input($faccount_id); 
echo $name .' Name'. 
    form_input($fname); 

$data = array('name' => 'submit', 'value' => 'Update Account', 'class' => 'submit'); 
echo form_submit($data); 

    ?> 
<?php echo form_close(); ?> 
+0

你會得到哪個錯誤? – Jens 2013-02-14 14:27:27

+0

我沒有收到錯誤消息。在表單變量'name'中是空白的,但'account_id'正在傳遞值。 – Gee 2013-02-14 14:40:40

回答

3

我相信get_where只是PREPS你查詢

$query->row_array()應將您的結果作爲array

$query = $this->db->get_where('db_accounts', array('account_id' => $account_id)); 
$result = $query->row_array(); 

對於問題的第二部分,看起來好像有很多事情要做。您的input函數中的$name的值是多少?你真的把值傳遞給input?確保名稱在您的input函數中設置,否則它將只是一個空字符串。

+0

+1,它看起來像缺少像上面那樣運行該查詢的代碼。 – djjjuk 2013-02-14 14:30:17

+0

我如何將account_id傳遞給下一頁而不是其他字段? – Gee 2013-02-14 14:32:47

+0

,因爲您已經擁有帳戶ID - 已傳入該函數。我編輯了上面的答案並加以註釋以突出顯示。 – djjjuk 2013-02-14 14:38:11

0

在djjjuk的幫助下找到了解決方案。

$query = $this->db->get_where('db_accounts', array('account_id' => $account_id)); 
    if ($query->num_rows() > 0) 
    { 
    $row = $query->row_array(); 

    } 

    $data['account'] = $row; 

    $data['fname']['value'] = $row['name']; 
    $data['faccount_id']['value'] = $account_id; 
    $data['name']  = ''; 
    $data['account_id'] = ''; 
} 

    $this->load->view('manage/input',$data);  

} 
2

從文檔佔用:

$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. This is useful if you need a compound select statement.

所以更換

$query = $this->db->select('name', $name); 

$this->db->select('name', $name); // No need to assign it to a variable 

然後$this->db->get_where();執行查詢並返回需要FETC整個查詢對象h從它的結果。

$query = $this->db->get_where('db_accounts', array('account_id' => $account_id)); 
$result = $query->row_array(); //For single row 
$result = $query->result_array(); //For more than one row