如何上传csv文件并将数据保存到我的mysql数据库中。 根据id.in cakephp 3 我无法做到这一点。谁能帮我。 我控制器 公共函数导入(){上传csv文件并将数据保存在cakephp中的数据库中3
if(isset($_POST["submit"])){
if($_FILES['file']['csv']){
$filename = explode('.', $_FILES['file']['csv']);
debug($filename);
if($filename[1]=='csv'){
$handle = fopen($_FILES['file']['csv'], "r");
while ($data = fgetcsv($handle)){
$item1 = $data[0];
// $item2 = $data[1];
// $item3 = $data[2];
// $item4 = $data[3];
$Applicants = $this->Applicants->patchEntity($Applicants, $item1);
$this->Applicants->save($Applicants);
}
fclose($handle);
}
}
}
$this->render(FALSE);
}
我的观点:
<div class="col-md-8">
<?= $this->Form->create('Applicants',['type' => 'file','url' => ['controller'=>'Applicants','action' => 'import'],'class'=>'form-inline','role'=>'form',]) ?>
<div class="form-group">
<label class="sr-only" for="csv"> CSV </label>
<?php echo $this->Form->input('csv', ['type'=>'file','class' => 'form-control', 'label' => false, 'placeholder' => 'csv upload',]); ?>
</div>
<button type="submit" class="btn btn-default"> Upload </button>
<?= $this->Form->end() ?>
</div>
我要上传CSV文件,并在我的database.but插入根据ID数据我是无法在cakephp中执行3 –
如果要逐行保存数据,是否要一次一行或全部插入数据,则可以在上述代码的while循环中使用newEntity而不是patch实体。 –
请检查我的编辑,这应该现在按预期工作 –