I have two tables that have (1:1) relationship between each others.
customers table:
- customerID (PK)(AI)
- customerName
- phone
addresses table:
- customerID (PK&FK)
- address
- city
- zipcode
I tried to update them in the same CodeIgniter view form.
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>
This is part of the my controller code:
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));
..
}
Note: up to this point everything was working for process the one table (customers).
This is the part of my model file:
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);
}
As for me the problem is the 'addresses' table need to customerID, but I don't insert it handly (auto_increment). I tried to many way for get it after inserted to customers table but I cant do it. Is there anybody know different way or what should I do in that way?