[英]how to using SQL INSERT INTO SELECT with codeigniter
如何在 CodeIgniter 中使用 SQL insert into select。
模型...
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;
}
首先從books
表中獲取所有書籍,然后根據您的$book_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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.