如何向<input>標簽中包含陣列值的資料庫插入一個值。我正在制作在線圖書館系統;這部分是我將一本書添加到資料庫中,有時一本書的作者不止一位。
現在我使用 implode 插入,但 2 位作者在 1 列中共享。當插入到 tbl_authors 時,我希望它們必須在不同的列中。我將書籍詳細資訊插入 tbl_books 和 tbl_authors 中的作者。我已經嘗試過每個但我只能在插入時顯示資料時這樣做我不知道
foreach($test as value)
{
'author_name' => $value[' no idea what i need to put here']
}



public function insertbooks()
{
$data=array(
'book_id' => $this->db->insert_id(),
'book_title' => $this->input->post('booktitle',true),
'section_id' => $this->input->post('section',true),
/*'book_author' => $this->input->post('bookauthor',true),*/
'book_serial' => $this->input->post('serial',true),
'book_qty' => $this->input->post('bookqty',true),
);
$sql1 = $this->db->insert('tbl_books',$data);
$author_name=implode(',', $this->input->post('bookauthor',true));
$data2=array(
'book_id' => $this->db->insert_id(),
'author_name' => $author_name
);
$sql2 = $this->db->insert('tbl_authors',$data2);
}
uj5u.com熱心網友回復:
您可能需要將每個作者插入為不同的行而不是列。
為此,您應該迭代作者陣列并分別進行每個插入,例如:
public function insertbooks()
{
$data = array(
'book_id' => $this->db->insert_id(),
'book_title' => $this->input->post('booktitle',true),
'section_id' => $this->input->post('section',true),
'book_serial' => $this->input->post('serial',true),
'book_qty' => $this->input->post('bookqty',true),
);
$sql1 = $this->db->insert('tbl_books',$data);
$book_id = $this->db->insert_id();
foreach ($this->input->post('bookauthor',true) as $author) {
$data2=array(
'book_id' => $book_id,
'author_name' => $author
);
$sql2 = $this->db->insert('tbl_authors',$data2);
}
}
這樣,您將擁有每本書/作者關系的單獨記錄。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438480.html
