我有这个网站http://mxcounters.com/traffictack/
用户来网站注册并登录。登录后,他们在自己的域中搜索一些seo评论。现在,我只想将每个用户的搜索分别存储到数据库中。然后,我会将每个用户搜索到的域显示到他们的帐户页面上。
我已经尝试了一些最后的活动方法来存储在数据库中,但它没有那么有用,请帮助我:
$time_since = now() - $this->session->userdata('last_activity');
$interval = 300;
// Do nothing if last activity is recent
if ($time_since < $interval) return;
// Update database
$updated = $this->db
->set('last_activity', now())
->where('id', $user_id)
->update('users');
试试这个
竞争对手
$userId = $this->session->userdata('user_id');
if(empty($userId)){
redirect('login'); # this for user not logged
}
else{
$search = $this->input->post('domain');
$resut = $this->method_name->serach_domain($userId, $search);
if(!empty($resut)){
#have data. if user come here set another methods for Insert data
$this->model_name->insert_search_result(($userId, $search);
}
else{
# nodata
echo "No result found"
}
}
在模型中
function insert_search_result(($userId, $search){
$data = array(
'user_id' => $userID ,
'search' => $search,
'timestamp' => date('Y-m-d H:i:s')
);
$this->db->insert('mytable', $data);
}
如果您想插入每个搜索,可以使用:
首先创建一个存储记录的表。
CREATE TABLE `usersearchs` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`user_id` VARCHAR(50) NOT NULL DEFAULT '0',
`search` VARCHAR(50) NOT NULL DEFAULT '0',
`time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX `id` (`id`)
)
ENGINE=InnoDB
;
在该表中,时间戳会自动添加到条目中。
然后在每次搜索中使用插入查询:
INSERT INTO usersearch (user_id, search) VALUES ($user_id, $search_string);
如果您只想更新last_activity列:
改变你的表,在每次表更新后,last_activity都会更新,并使用第二列,例如名为searchString的列来包含最后搜索的项。
ALTER TABLE `users`
CHANGE COLUMN `last_activity` `last_activity` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
ADD COLUMN `searchString` VARCHAR(50) NOT NULL AFTER;
然后在每次搜索中使用一个查询:
UPDATE users SET searchString = `$searchstring` WHERE id = $userID
使用数据库时请使用右转义(PDO准备的语句是一个不错的选择)