-1

My code is like this :

function insert_delete()
{
    ...

    $hotel             = $hotel;
    $address        = $address;
    $created_date     = date('Y/m/d H:i:s');

    $data = array('hotel'     => $hotel,
                  'address'     => $address,
                  'created_date' => $created_date
                 );
    $this->db->insert('tb_hotel',$data);

    $query = "DELETE FROM tbl_hotel WHERE created_date='$created_date'";
    $this->db>query($query);
}

When calling the insert_delete function, it will run the insert query and delete query.

How to do when insert the data, it is directly delete the data, but delete the data after half an hour?

Thank you

fusion3k
  • 11,259
  • 4
  • 22
  • 42
moses toh
  • 10,726
  • 57
  • 212
  • 388
  • Be super careful to use the [query binding feature of CodeIgniter](https://ellislab.com/codeigniter/user-guide/database/queries.html) and don't just jam in arbitrary data in your queries. – tadman Apr 07 '16 at 00:00

1 Answers1

1

I hope you dont mean you want the delete to actually run after half an hour, so I am assuming you want the delete to tidy up any rows that are more than 30 minutes old.

public function insert_delete()
{
    ...

    $hotel        = $hotel;
    $address      = $address;
    $created_date = date('Y/m/d H:i:s');

    $data = array('hotel'        => $hotel,
                  'address'      => $address,
                  'created_date' => $created_date
                 );
    $this->db->insert('tb_hotel',$data);

    // delete old rows more than 30 minutes old
    $date = new DateTime($created_date);

    $date->sub(new DateInterval('PT30M'));
    $del_dt = $date->format('Y-m-d H:i:s');

    $query = "DELETE FROM tbl_hotel WHERE created_date < '$del_dt'";
    $this->db>query($query);
}
RiggsFolly
  • 89,708
  • 20
  • 100
  • 143
  • I need you help. Look here : http://stackoverflow.com/questions/38175735/how-to-get-data-in-function-extend-controller – moses toh Jul 04 '16 at 02:22