gpt4 book ai didi

mysql - CodeIgniter:如何将具有一个名称和多个选择框的数据插入到第二个表中?

转载 作者:行者123 更新时间:2023-11-29 22:46:06 26 4
gpt4 key购买 nike

有一个注册表,该表单上有 5 个字段。其中之一是增加兴趣爱好。该字段是一个选择框,当用户想要添加多个爱好时,有一个 + 按钮可以生成一个新的选择框,用于添加第二个爱好等。

举个例子:

  Name:
Surname:
+ Hobbies: Select One
-fishing
-shopping
-etc.

单击加号时,会出现一个新的选择框。

  Name:
Surname:
Hobbies: fishing
-shopping
-etc.
+ Hobbies: Select One
-shopping
-etc.

所以选择框的代码如下。

我的查看文件

<?php
echo '<select name="hobby_id[]" class="select2" data-allow-clear="true" data-placeholder="Select One">';
echo '<option></option>';
echo '<optgroup label="Hobby List">';
foreach($hobbies as $hobby_item)
{
echo '<option value="'.$hobby_item->hobby_id.'">'.$hobby_item->hobby_name.'</option>';
}
echo '</optgroup>';
echo '</select>';
?>

我想从多个具有相同名称的选择框插入数据。但在我的模型文件中,我不确定如何使用它。我的模型.php

$this->db->set('name',  $this->input->post('name')); 
$this->db->set('surname', $this->input->post('surname'));
$this->db->set('phone', $this->input->post('phone'));
$this->db->set('mail', $this->input->post('mail'));

$this->db->insert('users');

$this->db->set('user_id', $this->db->insert_id());
$this->db->set('hobby_id', $this->input->post('hobby_id'));

$this->db->insert('hobbies');

我想要做的 View 如下所示。

user_id | hobby_id
1 1
2 1
2 2

我想添加具有相同名称的选择框的多行。但我不知道如何在模型文件中写入。你可以帮帮我吗?你能告诉我我可以在互联网上搜索的技巧吗?或者如果你有一个简单的例子,你可以分享。

最佳答案

您将获得爱好的值数组。因此,对于爱好,请在用户插入查询后在模型中尝试此操作:

$insert_id = $this->db->insert_id();
$hobby_ids = $this->input->post('hobby_id');
if(count($hobby_ids) > 0){
for($i=0;$i<count($hobby_ids);$i++) {
$this->db->set('user_id', $insert_id );
$this->db->set('hobby_id',$hobby_ids[$i]);
$this->db->insert('hobbies');
}
}

关于mysql - CodeIgniter:如何将具有一个名称和多个选择框的数据插入到第二个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29117109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com