簡體   English   中英

如何使用CodeIgniter插入多個表

[英]How to insert into multiple tables with CodeIgniter

我有兩個表,彼此之間有(1:1)的關系。

customers table:
 - customerID (PK)(AI)
 - customerName
 - phone

addresses table:
 - customerID (PK&FK)
 - address
 - city
 - zipcode

我試圖在相同的CodeIgniter視圖表單中更新它們。

update_view.php

<th>Customer Name:</th>
<td><input type="text" name="customerName"/></td>
<tr>
<th>Customer Phone:</th>
<td><input type="text" name="phone"/></td>
<tr>
<th>Address:</th>
<td><input type="text" name="address"/></td>
<tr>
<th>City:</th>
<td><input type="text" name="city"/></td>
<tr>
<th>Zip Code:</th>
<td><input type="text" name="zipcode"/></td>

這是我的控制器代碼的一部分:

    public function insert()
        {      
            $this->load->database();
            $this->load->model('my_model');
            $this->my_model->insert_entry();

            $custInsert=$this->my_model->get_all_customers();
            $this->load->view('main_view',array('customer'=>$custInsert));
..
}

注意:到目前為止,一切都在處理一個表(客戶)。

這是我的模型文件的一部分:

function insert_entry()
    {
        $this->customerName   = $_POST['customerName']; 
        $this->phone = $_POST['phone'];
        $this->db->insert('customers', $this); // up to here it was working

        //$customerID=$db->insert_id;
        $customerID=$this->db->query("SELECT MAX(customerID) FROM `customers`");
        $this->customerID;
        $this->address = $_POST['address'];
        $this->city = $_POST['city'];
        $this->zipcode = $_POST['zipcode'];
        $this->db->insert('addresses', $this);
}

至於我,問題是'address'表需要customerID,但我不會手動插入(auto_increment)。 插入到客戶表后我嘗試了很多方法來獲取它,但我不能這樣做。 是否有人知道不同的方式或我應該以這種方式做什么?

這樣做是個壞主意......當幾乎在同一時間添加新客戶時,您的應用程序將如何處理?

$customerID=$this->db->query("SELECT MAX(customerID) FROM `customers`");

你應該擺脫那條線,並使用首選和有效的方法。 如果不這樣做,它可能並且將不可避免地導致您在某個時間點為客戶取錯記錄並將地址與錯誤的客戶相關聯。

這是因為在同一時間(幾乎足夠)運行代碼的兩個客戶端可能在同一時間點遇到MAX() ,因此每個客戶端都可以獲得相同的值。 當他們都嘗試保存時,只有一個會成功,另一個會因主鍵約束而失敗。 這種情況被稱為競爭條件 ,應予以防范。

請改用:

 $this->db->insert_id()

這個:

$this->customerID;

應該:

$this->customerID = $this->db->insert_id();

實現您想要的更簡潔的方法是使用數組而不是$this 您將獲得所需的所有信息,僅此而已。 你的$this對象中有很多東西比你不需要的東西。

在你的控制器中

public function insert()
{      
    $this->load->database();
    $this->load->model('my_model');

    $data_user = array(
      'customerName' => $this->input->post('customerName'),
      'phone'        => $this->input->post('phone')
    );

    $data_address = array(
      'address'    => $this->input->post('address'),
      'city'       => $this->input->post('city'),
      'zipcode'    => $this->input->post('zipcode')
    );    

    $this->my_model->insert_entry($data_user, $data_address);

    [...]
}

在你的模型中

function insert_entry($data_user, $data_address) {

    $this->db->insert('customers', $data_user);

    $data_address['customerID'] = $this->db->insert_id();

    $this->db->insert('addresses', $data_address);
}

當你得到$_POST變量時,一個好的做法是使用Codeigniter輸入類 只需使用以下內容:

$this->input->post('VAR_NAME')而不是$_POST['VAR_NAME']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM