Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
hmm i cant use where i think because the insert row is not available for a check.i need just one row – dA_uNknOwN May 19 at 18:35
do you want to insert the array to just one row ?? – Charlie May 19 at 18:39
yes i need only one row. or line with every values in the right columns. somethin i made wrong – dA_uNknOwN May 19 at 18:41
table name is 'profile' isn't it? and What is the row name ? – Charlie May 19 at 18:43
table name is profile thats correct. but the new line or row has no name . the table looks so: columns are userid - public - gender - age - skills – dA_uNknOwN May 19 at 18:47
show 4 more comments

1 Answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.