在CodeIgniter中将值插入数据库;我有数组 标签
如何将值插入到 标记中具有数组值的数据库中。我正在制作在线图书馆系统;这部分是我向数据库添加一本书,有时一本书有多个作者。
现在我使用 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);
}
How can I insert a value to database having an array values in my <input>
tag. I'm making online library system; this part is I'm adding a book to database sometimes a book have more than 1 author.
Now I use implode to insert but the 2 authors are sharing in 1 one column. What I want they have to be in different column when inserting to tbl_authors. I insert book details to tbl_books and for authors in tbl_authors. I've tried for each but I can only do it when displaying data when insert I have no idea
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要将每个作者作为不同的行而不是列插入。
为了实现这一点,您应该迭代authors数组并单独插入每个元素,例如:
这样,您将为每本书/作者关系拥有单独的记录。
You'd probably need to have each author inserted as a different row not column.
To achieve this, you should iterate authors array and make each insert separately, like:
This way, you'll have separate record for each book / author relationshi.