CodeIgniter $ this-> db获取最后一个主键

时间:2019-03-07 04:30:44

标签: php codeigniter primary-key

我想获取表名为invoice_main的最后一个主键

$in=$this->db->insert_id();
redirect(base_url()."home/index/".$in); 

我在此行的$print = $this->db->insert('invoice_main',$datas);之后加上了代码

Public function insert_invoice()
        {

          $item_name = $_POST['item'];
          $rate = $_POST['rate'];
          $quantity = $_POST['quantity'];
          $tax = $_POST['tax'];
          $amount = $_POST['amount'];


          $invoice_id = $this->input->post('invoice_id');
          //$invoice_id++;
          $datas = array(
            'user_id' => $this->session->userdata('user_id'),
               'invoice_id' => $this->input->post('invoice_id'),
             'pt_opnum' => $this->input->post('pt_opnum'),
             'pt_uhid' => $this->input->post('pt_uhid'),
             'doc_name' => $this->input->post('doc_name'),
             'status'=>1
            );
          $print = $this->db->insert('invoice_main',$datas);
          for ($i=0; $i <count($item_name) ; $i++) {
            $data=array(
                'invoice_id' =>$invoice_id,
                'pt_name'=> $this->input->post('pt_opnum'),
                'date'=>date('d-m-Y'),
                'name'=>$item_name[$i],
                'rate'=>$rate[$i],
                'quantity'=>$quantity[$i],
                'tax'=>$tax[$i],
                'amount'=>$amount[$i],
                'sub_total'=>$_POST['sub_total'],
                'o_tax'=>$_POST['o_tax'],
                'grand_total'=>$_POST['grand_total'],
                'status'=>1
                );

            $this->db->insert('invoice_details',$data);


        }


        return($this->db->affected_rows()!=1)?false:true;


    }

2 个答案:

答案 0 :(得分:1)

我认为<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="260dp" android:layout_height="wrap_content" android:background="@android:color/white" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:gravity="center" android:minHeight="120dp" android:orientation="vertical" android:padding="20dp"> <ImageView android:id="@+id/icon" android:layout_width="80dp" android:layout_height="80dp" android:tint="@android:color/white" app:srcCompat="@drawable/info" /> <TextView android:id="@+id/Dialogtitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Latest @ LootBox" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textColor="@android:color/white" android:textStyle="bold" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|start" android:orientation="vertical" android:padding="20dp"> <TextView android:id="@+id/Dialogcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="3dp" android:text="All These Deals, Offers etc Are For Limited Period of Time And Can be Over at Any Time Without Any Prior Notice." android:textAlignment="center" android:textAppearance="@style/TextAppearance.AppCompat.Subhead" android:textColor="#666666" /> </LinearLayout> <LinearLayout android:id="@+id/lyt_action" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/bt_close" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/btn_rounded_green" android:paddingLeft="20dp" android:paddingRight="20dp" android:text="Get Started" android:textColor="@android:color/white" /> </LinearLayout> </LinearLayout> 没有返回值。获得插入是否成功,您应该使用:

private ImageView dialogBox;
Dialog customDialog;

customDialog = new Dialog(getActivity());

dialogBox = (ImageView) view.findViewById(R.id.dialogBox);
dialogBox.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        showDialog();
    }

});

private void showDialog() {
    customDialog.setContentView(R.layout.dialog_info);
}

答案 1 :(得分:1)

$this->db->insert('table_name',data);
$last_insert_id= $this->db->insert_id();