4

I don't want to update my timestamp field when update. I need to keep previous datetime in that field. But its updating automatically even I tried following

$data = array(
                'start_time' => $start,//previous datetime
                'user_id' => $agent_id,
                ....
            );
            $this->db->where('id', $chat_id)->update('chat_state', $data );

When I leave $start or force with previous datetime. Not work in both cases. Its updating with CURRENT TIMESTAMP in both cases

whats wrong here??

Rejoanul Alam
  • 5,435
  • 3
  • 39
  • 68

3 Answers3

2

Connect to your mysql server using MySQL Workbench, to check your table's schema, by running the following queries:

USE mydatabase;    
SHOW CREATE TABLE mytable;

I bet you'll see something like ON UPDATE CURRENT_TIMESTAMP associated with your start_time field.

Note that, relying on DBMS to auto-update values is not a good practice; It gives you surprises, and you end up spending hours to figure out the issue.

Let's remove the "ON UPDATE CURRENT_TIMESTAMP" clause, like this:

ALTER TABLE mytable MODIFY start_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP;

This should solve the problem.

Read more about TIMESTAMP's behaviors in different situations.

0

Follow these steps

  1. Select exist value (start_time)
  2. Update exist value to new field
  3. insert new value

01 Select current value in db

$query = $this->db->query("SELECT start_time FROM chat_state WHERE user_id = $chat_id");
$result = $query->result_array();

02 Update exist value

$data = array(
    'old_time' => $result[0]['start_time'] // move start_time to new field 
);
$this->db->where('id', $chat_id)->update('chat_state', $data );

03 insert new value

$data = array(
    'start_time' => date('Y-m-d H:i:s'),     // new Time stamp
    'user_id' => $agent_id
);
$this->db->where('id', $chat_id)->update('chat_state', $data );
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0

Probably you have the column type as DATETIME; you need to change the type to TIMESTAMP and default null. It will not be updated automatically.

Then, while inserting the data

'start_time'=>date("Y-m-d H:i:s")

And when you want to print it in view

date('d-M-y', strtotime($record['start_time']));
Scorpion
  • 396
  • 2
  • 14