Php 复选框的Codeigniter批量更新

Php 复选框的Codeigniter批量更新,php,sql,codeigniter,batch-updates,codeigniter-query-builder,Php,Sql,Codeigniter,Batch Updates,Codeigniter Query Builder,我有一个这样的情况,我想更新一些数据,其中的输入是复选框,我尝试了下面的代码。数据已保存在数据库中,但数据保存在另一行中 例如:我为BirdA选择了红色、黄色和灰色,同时为BirdD选择了深色和蓝色。 在数据库中,BirdA的颜色保存正确,但对于第二只鸟,黑色和蓝色保存在BirdB上(应保存在BirdD上) 我想解决上面的问题,所以数据应该保存在数据库的正确位置。请在下面检查我的代码: 我的数据库表: ============================================= |

我有一个这样的情况,我想更新一些数据,其中的输入是复选框,我尝试了下面的代码。数据已保存在数据库中,但数据保存在另一行中

例如:我为BirdA选择了红色、黄色和灰色,同时为BirdD选择了深色和蓝色。 在数据库中,BirdA的颜色保存正确,但对于第二只鸟,黑色和蓝色保存在BirdB上(应保存在BirdD上)

我想解决上面的问题,所以数据应该保存在数据库的正确位置。请在下面检查我的代码:

我的数据库表:

=============================================
| birds | red | blue | grey | yellow | dark | 
=============================================
| BirdA |  0  |   0  |   0  |   0    |   0  |
| BirdB |  0  |   0  |   0  |   0    |   0  |
| BirdC |  0  |   0  |   0  |   0    |   0  |
| BirdD |  0  |   0  |   0  |   0    |   0  |
=============================================
  <tr>
    <td><input type="hidden" name="birds[]" value='<?php echo $dp['birds']; ?>' /><?php echo $dp['birds']; ?></td>
    <td><p align='center'><input type="text" name="qtt[]" value='<?php echo $dp['qtt']; ?>' tabindex="2" style="width:30px;" /></td>
    <td><p align='center'><input type="checkbox" name="red[]" value='1' <?php if($dp['red']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="blue[]" value='1' <?php if($dp['blue']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="grey[]" value='1' <?php if($dp['grey']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="yellow[]" value='1' <?php if($dp['yellow']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="dark[]" value='1' <?php if($dp['dark']==1) { echo " checked=\"checked\""; } ?> /></td>
  </tr>
    $birds= $this->input->post("birds");
        if (empty($this->input->post("birds"))){
            $birds= 0;
        }
    $qtt= $this->input->post("qtt");
        if (empty($this->input->post("qtt"))){
            $qtt= 0;
        }
    $red= $this->input->post("red");
        if (empty($this->input->post("red"))){
            $red= 0;
        }
    $blue= $this->input->post("blue");
        if (empty($this->input->post("blue"))){
            $blue= 0;
        }
    $grey= $this->input->post("grey");
        if (empty($this->input->post("grey"))){
            $grey= 0;
        }
    $yellow= $this->input->post("yellow");
        if (empty($this->input->post("yellow"))){
            $yellow= 0;
        }
    $dark= $this->input->post("dark");
        if (empty($this->input->post("dark"))){
            $dark= 0;
        }

    for($x = 0; $x < sizeof($birds); $x++){
        $reslt[$x] = array(
            "birds"    => $birds[$x],
            "qtt"  => $qtt[$x],
            "red"  => $red[$x],
            "blue"  => $blue[$x],
            "grey"  => $grey[$x],
            "yellow"  => $yellow[$x],
            "dark"  => $dark[$x]
            );
        }
        $this->db->update_batch('db_birds', $reslt, 'birds');
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: x

Filename: controllers/....

Line Number: 613

Backtrace:

File: /home/.....
Line: 613
Function: _error_handler

File: /home/..../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 3

Filename: controllers/....

Line Number: 621

Backtrace:

File: /home/....
Line: 621
Function: _error_handler

File: /home/..../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 3

Filename: controllers/...

Line Number: 622

Backtrace:

File: /home/...
Line: 622
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 620

Backtrace:

File: /home/...
Line: 620
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 621

Backtrace:

File: /home/...
Line: 621
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 622

Backtrace:

File: /home/...
Line: 622
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 5

Filename: controllers/...

Line Number: 618

Backtrace:

File: /home/...
Line: 618
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

......
查看:

=============================================
| birds | red | blue | grey | yellow | dark | 
=============================================
| BirdA |  0  |   0  |   0  |   0    |   0  |
| BirdB |  0  |   0  |   0  |   0    |   0  |
| BirdC |  0  |   0  |   0  |   0    |   0  |
| BirdD |  0  |   0  |   0  |   0    |   0  |
=============================================
  <tr>
    <td><input type="hidden" name="birds[]" value='<?php echo $dp['birds']; ?>' /><?php echo $dp['birds']; ?></td>
    <td><p align='center'><input type="text" name="qtt[]" value='<?php echo $dp['qtt']; ?>' tabindex="2" style="width:30px;" /></td>
    <td><p align='center'><input type="checkbox" name="red[]" value='1' <?php if($dp['red']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="blue[]" value='1' <?php if($dp['blue']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="grey[]" value='1' <?php if($dp['grey']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="yellow[]" value='1' <?php if($dp['yellow']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="dark[]" value='1' <?php if($dp['dark']==1) { echo " checked=\"checked\""; } ?> /></td>
  </tr>
    $birds= $this->input->post("birds");
        if (empty($this->input->post("birds"))){
            $birds= 0;
        }
    $qtt= $this->input->post("qtt");
        if (empty($this->input->post("qtt"))){
            $qtt= 0;
        }
    $red= $this->input->post("red");
        if (empty($this->input->post("red"))){
            $red= 0;
        }
    $blue= $this->input->post("blue");
        if (empty($this->input->post("blue"))){
            $blue= 0;
        }
    $grey= $this->input->post("grey");
        if (empty($this->input->post("grey"))){
            $grey= 0;
        }
    $yellow= $this->input->post("yellow");
        if (empty($this->input->post("yellow"))){
            $yellow= 0;
        }
    $dark= $this->input->post("dark");
        if (empty($this->input->post("dark"))){
            $dark= 0;
        }

    for($x = 0; $x < sizeof($birds); $x++){
        $reslt[$x] = array(
            "birds"    => $birds[$x],
            "qtt"  => $qtt[$x],
            "red"  => $red[$x],
            "blue"  => $blue[$x],
            "grey"  => $grey[$x],
            "yellow"  => $yellow[$x],
            "dark"  => $dark[$x]
            );
        }
        $this->db->update_batch('db_birds', $reslt, 'birds');
A PHP Error was encountered
Severity: Notice

Message: Undefined variable: x

Filename: controllers/....

Line Number: 613

Backtrace:

File: /home/.....
Line: 613
Function: _error_handler

File: /home/..../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 3

Filename: controllers/....

Line Number: 621

Backtrace:

File: /home/....
Line: 621
Function: _error_handler

File: /home/..../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 3

Filename: controllers/...

Line Number: 622

Backtrace:

File: /home/...
Line: 622
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 620

Backtrace:

File: /home/...
Line: 620
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 621

Backtrace:

File: /home/...
Line: 621
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 4

Filename: controllers/...

Line Number: 622

Backtrace:

File: /home/...
Line: 622
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

A PHP Error was encountered
Severity: Notice

Message: Undefined offset: 5

Filename: controllers/...

Line Number: 618

Backtrace:

File: /home/...
Line: 618
Function: _error_handler

File: /home/.../index.php
Line: 315
Function: require_once

......

谢谢,向您致以最诚挚的问候

控制器代码:-

for($x = 0; $x < sizeof($birds); $x++){
        $reslt[$x] = array(
            "birds"    => $birds[$x],
            "qtt"  => $qtt[$x],
            "red"  => $red[$x],
            "blue"  => $blue[$x],
            "grey"  => $grey[$x],
            "yellow"  => $yellow[$x],
            "dark"  => $dark[$x]
            );
        }
$this->db->update_batch('db_birds', $reslt, 'birds');
 <?php


//sql Query.
//execute Query.


$ii=0;
foreach($dps as $dp){
      $iii = $ii++; 
 <tr>
    <td><input type="hidden" name="birds[<?php echo $iii;?>]" value='<?php echo $dp['birds']; ?>' /><?php echo $dp['birds']; ?></td>
    <td><p align='center'><input type="text" name="qtt[]" value='<?php echo $dp['qtt']; ?>' tabindex="2" style="width:30px;" /></td>
    <td><p align='center'><input type="checkbox" name="red[<?php echo $iii;?>]" value='1' <?php if($dp['red']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="blue[<?php echo $iii;?>]" value='1' <?php if($dp['blue']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="grey[<?php echo $iii;?>]" value='1' <?php if($dp['grey']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="yellow[<?php echo $iii;?>]" value='1' <?php if($dp['yellow']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="dark[<?php echo $iii;?>]" value='1' <?php if($dp['dark']==1) { echo " checked=\"checked\""; } ?> /></td>
  </tr>
  }
  
  
?>
for($x=0;$x$birds[$x],
“qtt”=>$qtt[$x],
“红色”=>$red[$x],
“蓝色”=>$blue[$x],
“灰色”=>$GRY[$x],
“黄色”=>$yellow[$x],
“暗”=>$dark[$x]
);
}
$this->db->update_batch('db_birds',$reslt,'birds');
视图代码中有更改:-

for($x = 0; $x < sizeof($birds); $x++){
        $reslt[$x] = array(
            "birds"    => $birds[$x],
            "qtt"  => $qtt[$x],
            "red"  => $red[$x],
            "blue"  => $blue[$x],
            "grey"  => $grey[$x],
            "yellow"  => $yellow[$x],
            "dark"  => $dark[$x]
            );
        }
$this->db->update_batch('db_birds', $reslt, 'birds');
 <?php


//sql Query.
//execute Query.


$ii=0;
foreach($dps as $dp){
      $iii = $ii++; 
 <tr>
    <td><input type="hidden" name="birds[<?php echo $iii;?>]" value='<?php echo $dp['birds']; ?>' /><?php echo $dp['birds']; ?></td>
    <td><p align='center'><input type="text" name="qtt[]" value='<?php echo $dp['qtt']; ?>' tabindex="2" style="width:30px;" /></td>
    <td><p align='center'><input type="checkbox" name="red[<?php echo $iii;?>]" value='1' <?php if($dp['red']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="blue[<?php echo $iii;?>]" value='1' <?php if($dp['blue']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="grey[<?php echo $iii;?>]" value='1' <?php if($dp['grey']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="yellow[<?php echo $iii;?>]" value='1' <?php if($dp['yellow']==1) { echo " checked=\"checked\""; } ?> /></td>
    <td><p align='center'><input type="checkbox" name="dark[<?php echo $iii;?>]" value='1' <?php if($dp['dark']==1) { echo " checked=\"checked\""; } ?> /></td>
  </tr>
  }
  
  
?>

>

>

>

>

> } ?>


那么您真正想要什么呢?我想将这些批处理数据(在复选框中)保存在数据库的正确位置将此
$reslt[$x]
更改为
$reslt[]
我已经尝试过了,什么都没有发生,没有保存您在
sizeof($birds)中得到的数据??错误
显示消息:未定义偏移量:3
消息:未定义变量:x
。。。看到这个截图了吗?你有其他的解决方案吗?兄弟@KUMAR?@karisma现在查看并检查我的更新答案,请告诉我发生了什么?这是工作,兄弟,但很少修改,谢谢兄弟@karisma很高兴听到你说它为你工作,享受和快乐编码。。。