1

テーブルにフィールドを挿入して ID を取得しようとしています (テーブルの PRIMARY KEY が AUTO INCREMENT であると仮定します)。そのレコードの自動増分 ID を 2 番目のテーブルに挿入する必要があります。明らかに $this->db->insert_id() を返すことができますが、その後 Controller の値にアクセスするにはどうすればよいですか?

変数で定義してコントローラーでアクセスしようとしましたが、うまくいきませんでした。

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: insert_id
Filename: controllers/POS.php
Line Number: 87

モデル:

function populate_pos_purchase_table($posPurchase) {
    $this->db->trans_begin();
    $this->db->insert('pos_purchase', $posPurchase);

    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;
        return true;
    }
}

コントローラ:

function update_payment_details() {

$newPayment = array (
    'pos_id' => $insert_id, // Line Number: 87
    'payment_description' => 'Point of Sales',
    'payment_method' => $this->input->post('payment_method'),
    'payment_date' => $this->input->post('payment_date'),
    'paid_amount' => $this->input->post('total'),
    'due_amount' => 0
);

$this->pos_model->populate_new_payment_table($newPayment);  
$this->index();

} 
4

1 に答える 1

1

最初の挿入からIDを保存してから、2番目に使用しますか?

function update_payment_details() {

$insert_id = populate_pos_purchase_table($posPurchase);

$newPayment = array (
    'pos_id' => $insert_id, // Line Number: 87
    'payment_description' => 'Point of Sales',
    'payment_method' => $this->input->post('payment_method'),
    'payment_date' => $this->input->post('payment_date'),
    'paid_amount' => $this->input->post('total'),
    'due_amount' => 0
);

$this->pos_model->populate_new_payment_table($newPayment);  
$this->index();

} 
于 2013-07-10T15:30:41.720 に答える