models 目錄
//新增
//方法一
public function create($title, $content)
{
//設值
$this->db->set('title', $title);
$this->db->set('content', $content);
$this->db->set('create_time', date('Y-m-d H:i:s'));
//存入資料庫
$this->db->insert('news');
// 產生: INSERT INTO news (title, content, create_time) VALUES ('$title', '$content', '2013-04-05 12:00:00')
}
//方法二
public function create($data)
{
// $data = array(
// 'title' => 'My title' ,
// 'name' => 'My Name' ,
// 'date' => 'My date'
// );
$this->db->insert('mytable', $data);
// 產生: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
}
//方法三
public function create($sql)
{
//$sql = "insert into tbl_user (name, age, groupname) values ('codeigniter, 35, 'Group 1')";
$this-> db-> query($sql);
}
註一
$sql = "INSERT INTO tbl_user (name, groupname, age) VALUES (".$this->db->escape($name).", ".$this->db->escape($groupname).".", ".$this->db->escape($age).")";
$this->db->query($sql);
//自動產生SQL字串function
$data = array('name' => $name,'email' => $email,'url' => $url);
$str = $this->db->insert_string('table_name',$data);
沒有留言:
張貼留言