【问题标题】:db->insert_id() Always Equals Zerodb->insert_id() 总是等于零
【发布时间】:2015-07-17 20:34:07
【问题描述】:

我的控制器中有一个 AJAX 函数:

public function add_display_row($shape, $rows) {
        $newRecord = array(
            'work_id' => '',
            'section_id' => (int)$rows + 1,
            'shape' => $shape
        );

        //insert new record after last
        $newro = array();
        for( $c=1; $c<6; $c++){
            $newRecord['ordinal'] = $c;
            $newRecord['size_id'] = $this->work_model->get_size_from_specs($shape, $c);
            $insNew = $this->work_model->save_new_featured_shape($newRecord);
            $newRecord['item_id'] = $insNew;
            array_push($newro, $newRecord);
        }

        print_r($newro);
    }

在模型中

public function save_new_featured_shape($record) {
      $this->db->trans_begin();
      $this->db->insert('work_featured', $record);

      if ($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        return false;
      } else {
        $this->db->trans_commit();
        $insert_id = $this->db->insert_id();
        return $insert_id;
      }
    }

插入所有 5 条记录后,我将数组返回给调用者,并且 item_id(应该是每个记录的插入 id)等于 0。

我需要返回 insert_id 来构建 DOM 元素,然后我将插入到 DOM 中。

谁能看出它为什么是零?

【问题讨论】:

  • 把你的数据插入数据库???
  • 数据正确插入数据库

标签: php codeigniter


【解决方案1】:

修改 add_display_row 如下。

   public function add_display_row($shape, $rows) {
    //$this->load->model('work_model');
    $newRecord = array(
        'work_id' => '',
        'section_id' => (int)$rows + 1,
        'shape' => $shape
    );
    $new_arr=array();
    //insert new record after last
    $newro = array();
    for( $c=1; $c<6; $c++){
        $newRecord['ordinal'] = $c;
        $newRecord['size_id'] =  $this->work_model->get_size_from_specs($shape, $c);
        $insNew = $this->save_new_featured_shape($newRecord);
        $new_arr['item_id'] = $insNew;
        $new_arr_arr=array_merge($newRecord,$new_arr);
        array_push($newro, $new_arr_arr);
    }
    echo "<pre>";
    print_r($newro);
    }

和模型功能如下。

  public function save_new_featured_shape($record) {
  $this->db->trans_begin();
  $this->db->insert('work_featured', $record);
    $insert_id = $this->db->insert_id();
  if ($this->db->trans_status() === FALSE) {
    $this->db->trans_rollback();
    return false;
  } else {
    $this->db->trans_commit();

    return $insert_id;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2016-04-06
    • 1970-01-01
    • 2017-08-28
    • 2015-12-13
    • 1970-01-01
    相关资源
    最近更新 更多