php - Determine if a New MySQL record was created or instead just updated? -


i have php function below creates new project task mysql database record if 1 same id not exist already.

if 1 exist same id, instead updates mysql record.

this code has lot of time invested important task record's date_modified field updated in event of these 5 fields have changed new value!:
- name
- description
- status
- type
- priority

if of these fields have same value previous value when making update, example if sort_order value changed not update date_modified field!

it works great after lots of work crafting work right. mention important not break functionality!

now question because function can create , update task records in mysql.

i have need know when each of these events happens.

if new task record created need know new record created.

if task record updated need know record had existed , updated.

is there anyway determine of 2 events happened have?

private function _addorupdatetaskrecord($taskid, $projectid, $created_by_user_id, $modified_user_id, $name, $description, $status, $priority, $type, $date_entered, $date_modified, $date_started, $date_completed, $date_due, $milestone_id, $assigned_user_id, $sort_order, $heading){      $sql = "         insert             $this->tasksdbtablename(task_id, project_id, created_by_user_id, modified_user_id, name, description, status, priority, type, date_entered, date_modified, date_started, date_completed, date_due, milestone_id, assigned_user_id, sort_order, heading)         values             ('$taskid', '$projectid', '$created_by_user_id', '$modified_user_id', '$name', '$description', '$status', '$priority', '$type', utc_timestamp(), utc_timestamp(), '$date_started', '$date_completed', '$date_due', '$milestone_id', '$assigned_user_id', '$sort_order', '$heading')         on duplicate key update             date_modified = (case                 when name <> values(name)                 or description <> values(description)                 or status <> values(status)                 or type <> values(type)                 or priority <> values(priority)                   utc_timestamp()                   else date_modified             end),             modified_user_id='$modified_user_id',             name='$name',             description='$description',             status='$status',             priority='$priority',             type='$type',             date_started='$date_started',             date_completed='$date_completed',             date_due='$date_due',             milestone_id='$milestone_id',             assigned_user_id='$assigned_user_id',             sort_order='$sort_order',             heading='$heading'";      $insertorupdatetasks = $this->db->query($sql);       return $insertorupdatetasks;  } 

update

i adding mysql triggers code here others see might someday similar issue, or myself don't end posting question have solution again!

trigger create event record when task created

drop trigger if exists project_task_new_event; create trigger `project_task_new_event` after insert on `apoll_web_projects_tasks` each row     insert apoll_web_projects_events (event_type, project_id, task_id, created_by_user_id, description, date_created) values ('6', new.project_id, new.task_id, new.modified_user_id, new.name, utc_timestamp()); 

trigger create event record when task updated

drop trigger if exists project_task_update_event; delimiter $$ create trigger `project_task_update_event` after update on `apoll_web_projects_tasks` each row begin if not (new.date_modified <=> old.date_modified)             insert apoll_web_projects_events (event_type, project_id, task_id, created_by_user_id, description, date_created) values ('7', new.project_id, new.task_id, new.modified_user_id, new.name, utc_timestamp());     end if; end $$ delimiter ; 

your code insecure. have sql injection problem.

to know if record created or updated, simple compare if create_date != update_date

when insert data, set create_date , update_date same value. in "on duplicate" section update "update_date" field.

another solution use triggers, this:

delimiter $$  create trigger `update_trigger` before update on `tasks_table` each row      begin         /* add fields want compare here */         if !(old.name <=> new.name or old.description <=> new.description or old.description <=> new.description or old.status <=> new.status or old.type <=> new.type or old.priority <=> new.priority)            new.date_modified = now()         end if;     end;$$  delimiter ; 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -