繁体   English   中英

在 CodeIgniter 中向数据库插入值; 我有数组<input>标签

[英]Insert value to database in CodeIgniter; I have array <input> tag

如何在<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']
    }

网页代码

添加书页

问题,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);
            }

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

为此,您应该迭代 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);
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM