gpt4 book ai didi

php - 如何使用codeigniter在mysql中插入多行

转载 作者:太空宇宙 更新时间:2023-11-03 10:44:35 25 4
gpt4 key购买 nike

我想使用提交按钮将多行插入到数据库中,这是我的 View 代码

    <?php   
foreach($role_array as $k =>$v)
{
//var_dump($k);
if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" )
{

?>
<div class="row-fluid">
<div class="span6">
<div class='control-group'>


<label for='<?php echo $k; ?>' class='control-label'><?php echo $v ;?></label>
<div class='controls'>
<select class='form-control' name='<?php echo $k; ?>[]' id='<?php echo $k; ?>' multiple='multiple' size='5'>
<?php

foreach($all_datas[$k] as $datas )
{

$username = "{$datas->nickname}";

echo "<option value='{$datas->id}'>{$username}</option>";
}
?>
</select>
<label for="<?php echo $k; ?>" class="error" style="display:none"></label>
</div>
</div>
</div>

</div>
<?php }} ?>

这是我的 Controller 代码

            $roles = $this->utility->roles;
foreach($roles as $k => $v)
{
if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k]))
{
$userrole = $k;
$post_user=isset($post_data[$k])?$post_data[$k]:'';

if (count($post_user) > 0)
{
for ($i = 0; $i < count($post_user); $i++)
{
$data = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]);
// var_dump($data);
$flag = true;



}
}
}
}

if(isset($flag))
{

$v=$this->JournalUsers->addJournalUser($data);
}

$data 输出是

    array(3) {
["journal_id"]=>int(83)
["user_role"]=>
string(15) "quality_checker"
["user_id"]=>
string(3) "136"
}
array(3) {
["journal_id"]=>
int(83)
["user_role"]=>
string(5) "texer"
["user_id"]=>
string(3) "103"
}
array(3) {
["journal_id"]=>
int(83)
["user_role"]=>
string(5) "texer"
["user_id"]=>
string(3) "133"
}
array(3) {
["journal_id"]=>
int(83)
["user_role"]=>
string(5) "texer"
["user_id"]=>
string(3) "132"
}
array(3) {
["journal_id"]=>
int(83)
["user_role"]=>
string(10) "typesetter"
["user_id"]=>
string(3) "109"
}

array(3) {
["journal_id"]=>
int(83)
["user_role"]=>
string(10) "typesetter"
["user_id"]=>
string(3) "153"
}

但是,当插入数据时,表中只会插入一行。

   array(3) {
["journal_id"]=>
int(83)
["user_role"]=>`enter code here`
string(10) "typesetter"
["user_id"]=>
string(3) "153"
}

我想将所有数据插入数据库

最佳答案

此处 $data 每次都重新初始化。像这样更新代码:

$data = array();
$roles = $this->utility->roles;
foreach($roles as $k => $v)
{
if($k != "bookin_operator" && $k!= "production_manager" && $k != "client" && $k != "admin" && isset($post_data[$k]))
{
$userrole = $k;
$post_user=isset($post_data[$k])?$post_data[$k]:'';

if (count($post_user) > 0)
{
for ($i = 0; $i < count($post_user); $i++)
{
$data[] = array("journal_id" => $new_journal_id,"user_role" => $userrole,"user_id" =>$post_user[$i]);
// var_dump($data);
$flag = true;



}
}
}
}

if(isset($flag) && !empty($data))
{

$v=$this->JournalUsers->addJournalUser($data);
}

在模型循环中是这样的:

public function addJournalUser($data) {
foreach ($data AS $row) {
// execute your insertion query with variable row
}
}

关于php - 如何使用codeigniter在mysql中插入多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33729517/

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