When my user login on in the activity's model. If the user has logged on before it updates that row belonging to the user id. Otherwise should Insert row.
Problem: Currently it just updates the same row as previous. It should insert a new row if user id has not logged on before.
How can I fix problem on model.
<?php
class Model_account_activity extends CI_Model {
public function updateActivity($key, $data) {
$data['user_id'] = $this->session->userdata('user_id');
if (isset($data['user_id'])) {
$user_id = $data['user_id'];
} else {
$user_id = 0;
}
$query = $this->db->query("UPDATE `" . $this->db->dbprefix . "user_activity` SET
`user_id` = '" . (int)$user_id . "',
`key` = " . $this->db->escape($key) . ",
`data` = " . $this->db->escape(serialize($data)) . ",
`date_added` = NOW()
");
if ($query == FALSE) {
$this->addActivity();
return true;
} else {
return false;
}
}
public function addActivity($key, $data) {
$data['user_id'] = $this->session->userdata('user_id');
if (isset($data['user_id'])) {
$user_id = $data['user_id'];
} else {
$user_id = 0;
}
$this->db->query("INSERT INTO `" . $this->db->dbprefix . "user_activity` SET
`user_id` = '" . (int)$user_id . "',
`key` = " . $this->db->escape($key) . ",
`data` = " . $this->db->escape(serialize($data)) . ",
`date_added` = NOW()
");
}
}