Ajax 照片上传和一些数据赢得';不行。我在模型中使用了batch_insert
这是View.php文件Ajax 照片上传和一些数据赢得';不行。我在模型中使用了batch_insert,ajax,codeigniter,Ajax,Codeigniter,这是View.php文件 <input name="u_code[]" required="required" style="margin:0px; "> <input name="u_name[]" required="required" style="margin:0px; "> <input name="u_address[]" required="required" style="margin:0px; "> <input name="photo
<input name="u_code[]" required="required" style="margin:0px; ">
<input name="u_name[]" required="required" style="margin:0px; ">
<input name="u_address[]" required="required" style="margin:0px; ">
<input name="photo[]" required="required" style="margin:0px; ">
这是我的控制器插入多个数据,但我想包括上传照片。不幸的是,它不会运行
我的表格中有一个字段,用于输入用户ID、用户代码、用户名、用户地址以及每个用户的照片
function user_add()
{
if ($_POST)
{
$u_id =$this->input->post('u_id');
$u_code =$this->input->post('u_code');
$u_name =$this->input->post('u_name');
$u_address = $this->input->post('u_address');
if(!empty($_FILES['photo']['name']))
{
$upload = $this->_do_upload();
$data['photo'] = $upload;
} $data = array();
for ($i = 0; $i < count($this->input->post('u_id')); $i++)
{
$data[$i] = array(
'u_id' => $u_id[$i],
'u_code' => $u_code[$i],
'u_name' => $u_name[$i],
'u_address' => $u_address[$i],
);
}
$insert = $this->user_model->user_add($data);
echo json_encode(array("status" => TRUE));
}
}
This part is the function to upload photo.
public function photo_upload()
{
$config['upload_path'] = 'upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100; //set max size allowed in Kilobyte
$config['max_width'] = 1000; // set max width image allowed
$config['max_height'] = 1000; // set max height allowed
$config['file_name'] = round(microtime(true) * 1000); //just milisecond timestamp fot unique name
$this->load->library('upload', $config);
if(!$this->upload->photo_upload('photo')) //upload and validate
{
$data['inputerror'][] = 'photo';
$data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
$data['status'] = FALSE;
echo json_encode($data);
exit();
}
return $this->upload->data('file_name');
}
**Model:**
public function user_add($data)
{
$this->db->insert_batch($this->table, $data);
return $this->db->insert_id();
}
函数用户添加()
{
如果(美元邮政)
{
$u_id=$this->input->post('u_id');
$u_code=$this->input->post('u_code');
$u_name=$this->input->post('u_name');
$u_地址=$this->input->post('u_地址');
如果(!空($_文件['photo']['name']))
{
$upload=$this->_do_upload();
$data['photo']=$upload;
}$data=array();
对于($i=0;$iinput->post('u_id'));$i++)
{
$data[$i]=数组(
“u_id”=>u_id[$i],
“u_代码”=>u_代码[$i],
“u_name”=>u_name[$i],
“u_地址”=>u_地址[$i],
);
}
$insert=$this->user\u model->user\u add($data);
echo json_编码(数组(“状态”=>TRUE));
}
}
这部分是上传照片的功能。
公共功能照片上传()
{
$config['upload_path']=“upload/”;
$config['allowed_types']='gif | jpg | png';
$config['max_size']=100;//设置允许的最大大小(以KB为单位)
$config['max_width']=1000;//设置允许的最大图像宽度
$config['max_height']=1000;//设置允许的最大高度
$config['file_name']=round(microtime(true)*1000);//唯一名称的时间戳仅为毫秒
$this->load->library('upload',$config);
如果(!$this->upload->photo_upload('photo')//上传并验证
{
$data['inputerror'][]='photo';
$data['error_string'][]=“Upload error:”。$this->Upload->display_errors(“”,);//显示ajax错误
$data['status']=FALSE;
echo json_编码($data);
退出();
}
返回$this->upload->data('file_name');
}
**型号:**
公共功能用户添加($data)
{
$this->db->insert_batch($this->table,$data);
返回$this->db->insert_id();
}
请先看这篇文章,这是一个平台,您可以从中获得关于您的问题的好建议。但对于这一点,你需要更具体地说明你在问什么?到目前为止你做了什么?在提问之前,请查看建议的SO问题并查看它们。尽管如此,您仍然没有找到解决方案,然后您可以在这里问一个问题因为无论是否有要上载的图像(
如果(!empty($\u FILES['photo']['name'])
),通过此初始化,您将覆盖$data['photo']
变量。在if
block之后,尝试设置else
blockelse{$data=[]}
。谢谢Tpojka,但仍然不起作用