Welcome Guest, Not a member yet? Register   Sign In
Taking Checkbox fields and writing them to the database.
#1

[eluser]maddtechwf[/eluser]
I have three db tables that are vehicle, users, and vehicles_to_users. When a new vehicle is being added they can select the driver for the vehicle. When they click Add, it writes a majority of the data to the vehicle db. Once it writes that data I get the insertion id so that I can write the user id and vehicle id to the vehicle_to_user database.

This is the code I have written in my model but it doesn't seem to be working when it comes to the vehicle_to_user db.

Code:
function insertVehicle($nickname, $year, $make, $model, $notes, $mileage, $users)
{
  $this->db->trans_start();
  
  $this->db->set('name', $nickname);
  $this->db->set('vyear', $year);
  $this->db->set('vmake', $make);
  $this->db->set('vmodel', $model);
  $this->db->set('notes', $notes);
  $this->db->set('initial_mileage', $mileage);
  $this->db->insert('vehicle');
  $vehicle_id = $this->db->insert_id();
  
  foreach($users->result() as $user)
  {
   $this->db->set('user_id', $user);
   $this->db->set('vehcile_id', $vehicle_id);
   $this->db->insert('vehicle_to_user');
  }

  $this->db->trans_complete();
}

View Code (No issues here with the available users showing)
Code:
<label for="">Drivers (Users)</label>
            &lt;?php foreach ($users as $user): ?&gt;
             &lt;?php echo form_checkbox('vehicle_drivers', "{$user["id"]}"); ?&gt;&lt;?php echo "{$user["first_name"]} {$user["last_name"]}"; ?&gt;
            &lt;?php endforeach; ?&gt;

Controller
Code:
public function add()
  {
   $nickname = $this->input->post('vehicle_name');
   $year = $this->input->post('vehicle_year');
   $make = $this->input->post('vehicle_make');
   $model = $this->input->post('vehicle_model');
   $notes = $this->input->post('vehicle_notes');
   $mileage = $this->input->post('vehicle_init_mileage');
   $users = $this->input->post('vehicle_users');
  
   $this->vehicle_m->insertVehicle($nickname, $year, $make, $model, $notes, $mileage, $users);
  }


Messages In This Thread
Taking Checkbox fields and writing them to the database. - by El Forum - 04-19-2014, 02:10 PM



Theme © iAndrew 2016 - Forum software by © MyBB