확인란에 대한 Codeigniter 일괄 업데이트

Nov 02 2020

나는 이와 같은 상황이 있습니다. 입력이 확인란 인 일부 데이터를 업데이트하고 싶습니다.이 코드를 아래에서 시도했습니다. 데이터는 이미 데이터베이스에 저장되었지만 다른 행에 저장된 데이터,

예 : BirdA에 대해 빨간색, 노란색 및 회색을 확인하고 동시에 BirdD에 대해 진한 파란색을 확인했습니다. 데이터베이스에서 BirdA의 색상은 올바르게 저장되었지만 두 번째 새의 경우 BirdB에 진한 파란색이 저장되었습니다 (BirdD에 저장해야 함 )

위의 문제를 해결하고 싶으므로 데이터가 데이터베이스의 올바른 위치에 저장되어야합니다. 아래 내 코드를 확인하십시오.

내 DB 테이블 :

=============================================
| 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');

해당 코드를 실행하면 PHP에 다음과 같은 오류가 발생합니다.

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

......

감사합니다. 항상 좋은 일만 있기를 바랍니다

답변

1 KUMAR Nov 03 2020 at 00:33

컨트롤러 코드 :-

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>
  }
  
  
?>