Php 如何使用带有codeigniter的SQL插入选择
如何在CodeIgniter中选择SQL插入 模型Php 如何使用带有codeigniter的SQL插入选择,php,sql,codeigniter,Php,Sql,Codeigniter,如何在CodeIgniter中选择SQL插入 模型 public function history($book_id) { $query = $this->db->query('INSERT orders (book_id, title) SELECT book_id, book_title FROM books WHERE book_id =
public function history($book_id)
{
$query = $this->db->query('INSERT orders (book_id, title)
SELECT book_id, book_title
FROM books
WHERE book_id = \'$book_id\'');
return true;
}
首先从
图书
表中获取所有图书,然后根据$book\u id
示例:
public function history($book_id)
{
$this->db->select('book_id, book_title');
$this->db->from('books');
$this->db->where('book_id', $book_id);
$query = $this->db->get();
if ( $query->num_rows() > 0 ) // if result found
{
$row = $query->result_array(); // get result in an array format
$data = array();
foreach($row as $values){
$data = array(
'book_id' => $values['book_id'],
'title' => $values['book_title']
);
$this->db->insert('orders', $data); // insert in order table
}
return true;
}
else{
return false;
}
}
多谢各位much@tianjinchen当前位置很高兴帮助您不要忘记接受答案这将对未来的访客有所帮助