[英]Mysql INSERT and SELECT in CodeIgniter
I have 3 tables a many -to-many, 我有3对多桌
BOOK 书
book_id | book_name | rubric | autor
RUBRIC 专栏
rubric_id | rubric_name
AUTOR AUTOR
autor_id | first_name | last_name
I looking for query that select all data in one table. 我在寻找查询,以选择一个表中的所有数据。 I insert data via form like this: 我通过这样的形式插入数据:
Controller 调节器
public function insert_data_to_db(){
$form_data_book_name['book_name'] = $this->input->post('book_name');
$form_data_autor['first_name'] = $this->input->post('first_name');
$form_data_autor['last_name'] = $this->input->post('last_name');
$form_data_rubric['rubric'] = $this->input->post('book_rubric');
$proc = $this->book_model->insert_books_to_db($form_data_book_name);
if($proc){
echo "ok";
}
$proc1 = $this->book_model->insert_autors_to_db($form_data_autor);
if($proc1){
echo "ok";
}
$proc2 = $this->book_model->insert_rubric_to_db($form_data_rubric);
if($proc2){
echo "ok";
}
}
MODEL 模型
public function insert_books_to_db($form_data_book_name){
return $this->db->insert('book',$form_data_book_name);
}
public function insert_autors_to_db($form_data_autor){
return $this->db->insert('autor',$form_data_autor);
}
public function insert_rubric_to_db($form_data_rubric){
return $this->db->insert('book',$form_data_rubric);
}
And to select data I use query in my Model like this: 为了选择数据,我在模型中使用查询,如下所示:
public function show_book_and_autor_name(){
$query = $this->db->query("SELECT DISTINCT book.book_name, autor.first_name, autor.last_name, rubric.rubric_id FROM book LEFT JOIN autor ON book.autor=autor_id LEFT JOIN rubric ON book.rubric=rubric_id ORDER BY book.book_id;");
return $query->result();
}
How to insert in column autor | rubric
如何在列autor | rubric
插入autor | rubric
autor | rubric
some keys with relations with table AUTOR and RUBRIC? autor | rubric
一些与表AUTOR和RUBRIC有关系的钥匙? And How to select it in right way? 以及如何正确选择它? Thank you in advance! 先感谢您!
In your models you could use the function: 在模型中,您可以使用以下功能:
$this->db->insert_id();
to get the inserted ID. 获取插入的ID。 You could do: 您可以这样做:
public function insert_books_to_db($form_data_book_name){
if ( $this->db->insert('book',$form_data_book_name) )
return $this->db->insert_id();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.