The following code uploads multiple images no problem. However, I'm trying to get it to update a field in a table based on what iteration the loop is in. PROBLEM: The IF Statement seems to not work when looped. I.e. it only adds the first file_name to the database.
Anyone see what I'm doing wrong here? Much appreciated if so!!!
for ($i = 1; $i < 4; $i++)
{开发者_运维百科
/* Handle the file upload */
$upload = $this->upload->do_upload('image' . $i);
/* File failed to upload - continue */
if ($upload === FALSE)
continue;
/* Get the data about the file */
$data = $this->upload->data();
$uploadedFiles[$i] = $data;
if ($i == 1)
{
$filenames1 = array(
'product_image_front' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames1);
}
if ($i == 2)
{
$filenames2 = array(
'product_image_back' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames2);
}
if ($i == 3)
{
$filenames3 = array(
'product_image_back' => $data['file_name'],
);
$this->db->where('id', $this->db->insert_id());
$this->db->update('products', $filenames3);
}
}
insert_id - Get the ID generated in the last query.
Store it in a variable before the loop.
精彩评论