Mysql – Codeigniter – Input post array insert into thesql database

arrayscodeignitermysql

I dont understand how to exactly insert a row into a MySQL table. If I use my code, I add a new row for every post. Rather than that, I want only one row with all the values.

Here is the code sample.

HTML:

echo form_open('account/update'); 
  echo "<p><label for='gender'>Gender</label>
 <input type='text' name='gender' id='gender'  value='".$gender."'  />  
  </p>
  <p>
  <label for='age'>Age</label>
  <input type='text' name='age' id='age' value='".$age."' />
  </p>
      <p>
  <label for='bio'>Biografie</label>
  <input type='text' name='bio' id='bio' value='".$bio."' />
  </p>
      <p>
  <label for='skills'>Skills</label>
  <input type='text' name='skills' id='skills' value='".$skills."' />
  </p>

  <p><input type='submit' value='Save' /></p>";


 echo form_close(); 

Controller:

function update() 
{   
    $userid = $this->session->userdata("userid");


    $datanew = array(
           'userid' => $this->session->userdata("userid"),
           'gender' => $this->input->post('gender'),
           'age' => $this->input->post('age'),
           'bio' => $this->input->post('bio') ,
           'skills' => $this->input->post('skills')
        );
$this->session->set_userdata($data);
    $this->load->model('model_account');
    $this->load->model("model_user");

    $this->model_account->profile_insert($datanew);

$this->load->view("change_avatar");
    redirect("account/show/".$this->session->userdata("userid"));
}

Model:

function profile_insert($datanew) 
{   

  $this->db->insert('profile', $datanew);

}

I get 5 rows if I submit the HTML form.

Best Solution

This works for me: I Set the userid to unique and made a if statement in the controller ti switch between a insert and update function.

Controller:

    function update() 
{   

    $datanew = array(
           'userid' => $this->session->userdata("userid"),
           'gender' => $this->input->post('gender'),
           'age' => $this->input->post('age'),
           'bio' => $this->input->post('bio') ,
           'skills' => $this->input->post('skills')
        );

    $exists = $this->db->select('profile')->where('userid', $this->session->userdata("userid"));

    if($exists)
    {

      $this->session->set_userdata($datanew);
      $this->load->model('model_account');
      $this->load->model("model_user");
      $this->model_account->profile_update($datanew);
      $this->load->view("change_avatar");
      redirect("account/show/".$this->session->userdata("userid"));
    } 
      else
    {


      $this->session->set_userdata($datanew);
      $this->load->model('model_account');
      $this->load->model("model_user");
      $this->model_account->profile_insert($datanew);     
      $this->load->view("change_avatar");
      redirect("account/show/".$this->session->userdata("userid"));
    }   

}

model:

function profile_insert($datanew) 
{   
        $this->db->insert('profile', $datanew);          
}

    function profile_update($datanew) 
{   
        $this->db->update('profile', $datanew);              
}

Thanks for helping me