Welcome Guest, Not a member yet? Register   Sign In
SQL ON DUPLICATE KEY
#1

[eluser]chandlerou[/eluser]
Trying to use Codeigniter to check to see if a record already exists.

Code:
$this->db->from('team');
$this->db->where('id', team['id']);                    
if($this->db->count_all_results()){
    $this->db->where('id', $team['id']);
    $this->db->update('team', $team);
}else{
    $this->db->insert('team', $team);
}

I know I have to use the ON DUPLICATE KEY, but I am unable to find a way to utilize that with an associative array. Does anyone know how? I'm trying not to hand write each field, or use a function to expand the array (http://www.iamboredsoiblog.eu/2010/10/28...duplicate/) Also, do I need that duplicate WHERE clause, in the TRUE logic?
#2

[eluser]oppenheimer[/eluser]
If the record doesn't already exist, then wouldn't $team['id'] be blank?
If so, you could determine if a record already exists by checking if $team['id'] is greater than 0.
#3

[eluser]InsiteFX[/eluser]
Code:
$this->db->where('id', team['id']);
$query = $this->db->get('team');

if ($query->num_rows() > 0)
{
    // Record exists!
}
else
{
    // Record doe's not exist!
}

InsiteFX




Theme © iAndrew 2016 - Forum software by © MyBB