在CodeIgniter中将值插入数据库;我有数组 标签

发布于 2025-01-11 19:05:48 字数 1802 浏览 0 评论 0原文

如何将值插入到 标记中具有数组值的数据库中。我正在制作在线图书馆系统;这部分是我向数据库添加一本书,有时一本书有多个作者。

现在我使用 implode 插入,但 2 位作者在 1 列中共享。我希望插入到 tbl_authors 时它们必须位于不同的列中。我将书籍详细信息插入到 tbl_books 中,并将作者详细信息插入到 tbl_authors 中。我已经尝试过每个,但我只能在插入时显示数据时执行此操作我不知道

   foreach($test as value)
   {
   'author_name' => $value[' no idea what i need to put here']
    }

Html 代码

添加图书页面

问题,2 位作者共享 1 列,我需要将它们分开

  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']
    }

Html Code

Add Book Page

Problem, 2 authors sharing on 1 column. I need To separate them

  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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅我要高飞 2025-01-18 19:05:48

您可能需要将每个作者作为不同的行而不是列插入。

为了实现这一点,您应该迭代authors数组并单独插入每个元素,例如:

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);
    }
}

这样,您将为每本书/作者关系拥有单独的记录。

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:

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);
    }
}

This way, you'll have separate record for each book / author relationshi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文