0

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.

9
  • hmm i cant use where i think because the insert row is not available for a check.i need just one row Commented May 19, 2013 at 18:35
  • do you want to insert the array to just one row ?? Commented May 19, 2013 at 18:39
  • yes i need only one row. or line with every values in the right columns. somethin i made wrong Commented May 19, 2013 at 18:41
  • table name is 'profile' isn't it? and What is the row name ? Commented May 19, 2013 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 Commented May 19, 2013 at 18:47

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.