在codeigniter中批量更新所有表单输入数据
updating all form input data in batch in codeigniter
我想弄清楚为什么我在 codeigniter 中的批量更新不起作用。
这是我的代码:
<?php echo form_open('about/update_approach',array('class' => 'approach-form')) ?>
<? foreach ($approach_content as $i=>$approach): ?>
<input type="hidden" name="id[]" value="<?= $approach['id'] ?>">
<div class="admin-group ">
<div class="admin-form-item">
<label for="title">Title</label>
<input type="text" maxlength="128" value="<?= $approach['title']; ?>" required name="title[]">
</div>
<div class="admin-form-item">
<label for="title">Text</label>
<input type="text" maxlength="256" value="<?= $approach['text']; ?>" required name="text[]">
</div>
</div>
<? endforeach ?>
<?php echo form_submit($dataSubmit);?>
<?php echo form_close(); ?>
型号:
public function update_approach(){
$data = $this->input->post(NULL,TRUE);
$this->db->update_batch('1985_approach', $data, 'id');
}
但是如果我这样做,我会收到错误消息:
One or more rows submitted for batch updating is missing the specified index.
如果我做 print_r($this-input->post());
<?php
Array(
[id] => Array(
[0] => 1[1] => 2[2] => 3[3] => 4[4] => 5[5] => 6
) [title] => Array(
[0] => CREATIVEZ[1] => CURATION[2] => ARTISTS[3] => PRODUCTION[4] => ENGAGEMENT[5] => RESULTS
) [text] => Array(
[0] => Fromconceptthroughexecution,
ourcreatorsarethecreativedirectors,
thecontentmakers and theconversationalists . [1] => Loremipsumdolorsitamet,
consecteturadipiscingelit . Donecpretiumsemperaugue,
ascelerisquearculuctusvitae . Inporttitorleo . [2] => LoremipsumIndolorminimmagnaDuisexquieaintempor . [3] => LoremipsumReprehenderitquiautedo utlaborumoccaecatametconsecteturincididuntelitenimeiusmodvoluptatedo aliqua . [4] => LoremipsumExercitationestdolorminimdoloreadidnonlaborismollitExcepteurnostrudexadipisicingdoloreculpa . [5] => LoremipsumNoneaetvelitaliquipsedculpadoloranimdolormagnaintemporid .
)
)
很久没用Code Igniter了。
我检查了文档,你发现了这个:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
您确实拥有正确的数据,但错误的结构。将数据转换为:
array(
array(
'id'=>1,
'title'=>'CREATIVEZ',
'text'=>'some text'
),
array(
'id'=>2,
title'=>'CURATION',
'text'=>'some text'
)
);
在PHP中你经常使用关联数组而不是传统的对象。
因此,将 php 数组的单个条目视为传统对象。
我想弄清楚为什么我在 codeigniter 中的批量更新不起作用。
这是我的代码:
<?php echo form_open('about/update_approach',array('class' => 'approach-form')) ?>
<? foreach ($approach_content as $i=>$approach): ?>
<input type="hidden" name="id[]" value="<?= $approach['id'] ?>">
<div class="admin-group ">
<div class="admin-form-item">
<label for="title">Title</label>
<input type="text" maxlength="128" value="<?= $approach['title']; ?>" required name="title[]">
</div>
<div class="admin-form-item">
<label for="title">Text</label>
<input type="text" maxlength="256" value="<?= $approach['text']; ?>" required name="text[]">
</div>
</div>
<? endforeach ?>
<?php echo form_submit($dataSubmit);?>
<?php echo form_close(); ?>
型号:
public function update_approach(){
$data = $this->input->post(NULL,TRUE);
$this->db->update_batch('1985_approach', $data, 'id');
}
但是如果我这样做,我会收到错误消息:
One or more rows submitted for batch updating is missing the specified index.
如果我做 print_r($this-input->post());
<?php
Array(
[id] => Array(
[0] => 1[1] => 2[2] => 3[3] => 4[4] => 5[5] => 6
) [title] => Array(
[0] => CREATIVEZ[1] => CURATION[2] => ARTISTS[3] => PRODUCTION[4] => ENGAGEMENT[5] => RESULTS
) [text] => Array(
[0] => Fromconceptthroughexecution,
ourcreatorsarethecreativedirectors,
thecontentmakers and theconversationalists . [1] => Loremipsumdolorsitamet,
consecteturadipiscingelit . Donecpretiumsemperaugue,
ascelerisquearculuctusvitae . Inporttitorleo . [2] => LoremipsumIndolorminimmagnaDuisexquieaintempor . [3] => LoremipsumReprehenderitquiautedo utlaborumoccaecatametconsecteturincididuntelitenimeiusmodvoluptatedo aliqua . [4] => LoremipsumExercitationestdolorminimdoloreadidnonlaborismollitExcepteurnostrudexadipisicingdoloreculpa . [5] => LoremipsumNoneaetvelitaliquipsedculpadoloranimdolormagnaintemporid .
)
)
很久没用Code Igniter了。 我检查了文档,你发现了这个:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
您确实拥有正确的数据,但错误的结构。将数据转换为:
array(
array(
'id'=>1,
'title'=>'CREATIVEZ',
'text'=>'some text'
),
array(
'id'=>2,
title'=>'CURATION',
'text'=>'some text'
)
);
在PHP中你经常使用关联数组而不是传统的对象。 因此,将 php 数组的单个条目视为传统对象。