dpge74512 2011-07-16 17:27
浏览 57
已采纳

只有3个中的第一个IF语句在PHP循环中执行

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);
  }

}
  • 写回答

1条回答 默认 最新

  • douji1877 2011-07-16 17:49
    关注

    insert_id - Get the ID generated in the last query.

    Store it in a variable before the loop.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据