public function insert_tmp_post($data = array()) { $this->db->set($data); $this->db->insert($this->db->dbprefix . 'post'); return $this->db->insert_id(); }
public function update_tmp_post($data = array()) { $this->db->set($data); $this->db->where('post_id', $this->session->userdata('post_id')); $this->db->update($this->db->dbprefix . 'post'); }
public function delete_post() { $this->db->where('post_id', $this->session->userdata('post_id')); $this->db->delete($this->db->dbprefix . 'post');
$this->session->unset_userdata('post_id'); }
public function clear_unconfirmed_post() {
} }
Controller
PHP Code:
<?php
class Newthread extends MX_Controller {
public function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->helper('string'); $this->load->model('catalog/forum/newthread_model'); }
public function index($forum_id = NULL) { $code = random_string('alpha', 32);
hye.
If i understand your issue, you want to delete all rows where date_modified (field in your table) are older than 15 minutes.
If you want it at every request who call your controller/method, you take the time at the controller access and you make a sql query which compare the time now et the time with date_modified. If delta between now and date_mdified > 15 minutes your delete the row. Use timestamp and you compare (15*60 seconds) between.
See sql query delete and had when your delta > 15*60 seconds.
If you want to delete rows when 15 minutes have passed without call your controller/method, develop a trigger in your database for example or a method using javascript for example combined with php to have the elapsed time, and after make your removal request.
Advices in your code : you will protect it more, one example among others, use $this->input->post('data', TRUE) and add rules for your form validation and perhaps use config file to store rules for your form validation.
(02-05-2017, 03:29 AM)Diederik Wrote: Just perform a single sql
Code:
DELETE FROM post
WHERE date_modifieda < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE))
I tried this but does not work
PHP Code:
public function clear_unconfirmed_post() { $this->db->where('code is NOT NULL', NULL, FALSE); $this->db->where('date_modified < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE))'); $this->db->delete($this->db->dbprefix . 'post'); }
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!
This will override the normal syntax of db->where, which takes 2 parameters: fieldname and conditional value.
I did not work I placed the function in the construct area so once I reload page it checks it.
PHP Code:
public function __construct() { parent::__construct(); $this->load->model('catalog/forum/newthread_model'); $this->newthread_model->clear_unconfirmed_post(); }
What I am trying to achieve is if there is any columns in my table that still have a code present after 15 min then will remove that row.
date_modified looks like 2017-02-16 13:53:18
PHP Code:
public function clear_unconfirmed_post() { $this->db->where('code is NOT NULL', NULL, FALSE); $this->db->where('date_modified < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 15 MINUTE))', NULL, FALSE); $this->db->delete($this->db->dbprefix . 'post'); }
There's only one rule - please don't tell anyone to go and read the manual. Sometimes the manual just SUCKS!
$dt = new Datetime(); //create object for current date/time $dt->modify('15 minutes ago'); //substract 15 minutes $sdt = $dt->format('Y-m-d H:i:s'); //format it into a datetime string