CodeIgniter Forums

Full Version: appending record instead of replacing field
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to update one field of a table.
PHP Code:
       $userid=$this->session->userdata('userid');
 
       $campaign=$this->session->userdata('campaign');
 
       $sql="SELECT * FROM users WHERE email = ?";
 
       $query $this->db->query($sql,$userid);
 
       $row $query->row();
 
       $iRc=$this->db->replace('users',array('campaign'=> $campaign)); 
I am finding that this is appending a new record onto the end of the table rather than replacing the campaign field if the userid matches. This is the table structure. What is the problem?
@richb201,

Based on the CI documentation ( https://www.codeigniter.com/user_guide/d...ating-data ) replace is basically the SQL standard for (optional) DELETE + INSERT, using PRIMARY and UNIQUE keys...

What you need to do is what you stated in your first sentence. You need to do an update to the table on a specific column where email equals a specific email address.
Thanks. I did see that. I was thinking that perhaps the issue was that the field I am trying to update is not primary. I did make it unique. You can see that the id is already primary and I can't have two primaries. Is there a better way to do this other than replace? Or should I manually do a delete and then an insert?
I solved it by using:

$userid=$this->session->userdata('userid');
$campaign=$this->session->userdata('campaign');
$this->db->set('campaign', $campaign);
$this->db->where('email', $userid);
$this->db->update('users');
@richb201,

Perfect.