Re: [請益] 初學CodeIgniter的一些問題
※ 引述《knives ()》之銘言:
: 我現在開始學著用codeigniter寫php
: 可是我有一些疑問
: 就是如果我在application裡面的物件有一個function 叫做onedit,是用來處理表單
: , 其中將$_POST 變數用參數的方式傳進去
: class Blog extends Controller{
: function onedit($arr = array())
: {
: //將$_POST
: $this->db->insert('tbl',$arr);
: //官方範例是這樣寫
你的寫法沒有錯誤,不過你取得 $_GET 和 $_POST 的方法不對/不好。
: //$this->db->insert('tbl',$_POST);
: }
: }
: 如果是一般的php的話
: 我會寫成這樣
: $obj = new Blog();
: $obj->onedit($_POST);
: 可是如果換成用codeigniter的話,那我要怎麼讓$_POST變數傳入到onedit裡
: 我在輸入的頁面的時候,使用
: <?=form_open('blog/onedit')?>
這是用 form helper 傳入到 blog 這個 controller 裡面的 onedit method 。
取得 $_POST 可以用 $value = $this->input->post('name');
或用 form_validation ( it's much better )
取得 $_GET 則是用 blog::onedit($value);
你可看一下 system\libraries\Input.php 。可以知道
當 $config['enable_query_strings'] === FALSE 的時候 $_GET 會被重設為 array()
(根據 config 系統預設是 FALSE)
至於 $_POST 則是一定會透過 $_POST = $this->_clean_input_data($_POST);
這個動作來做 filtering
: 可是這樣的話,onedit裡面的$arr,要怎麼知道它的值是參考POST變數
補充
→
,
→
,
你不能這樣做,,根據你的需求,要改成這樣
Class foo extends Controller {
function __construct() { parent::Controller(); }
function update()
{
$data = array(
'column1' => $this->input->post('value1');
'column2' => $this->input->post('value2');
)
echo ($this->do_update($data)) ? 'ok' : 'failed' ;
}
function do_update($array)
{
reutrn ($this->db->insert("table",$array)) ? TRUE : FALSE ;
}
}
但要記得做 input filtering.
※ 編輯: knives 來自: 203.222.21.71 (04/24 10:10)
推
,
不是,在 CodeIgniter 裡面 DB 是一個 Library
如果你沒有在 autoload 裡面載入,你必須手動 $this->load->database();
這樣你的 $this->db->query(); 才會有用,要不然是 undefined method.
另外
$this->load->model('Model_name', 'example');
$this->example->function(); 才是在操作 Model 。
請先把文件全部看完,就不會有這些疑問了
http://codeigniter.com/user_guide/
推
,
→
,
雖然透過 DBI 來做是相對比較安全,但這種作法.....嘖嘖.....
--
The Internet: where men are men, women are men, and children are FBI agents.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.220.104.63
※ 編輯: roga 來自: 61.220.104.63 (04/24 15:50)
討論串 (同標題文章)
PHP 近期熱門文章
PTT數位生活區 即時熱門文章