insert user id into DB while sending a new post
- Single Page
Posted 04 September 2012 - 20:29 PM
in my cms i allow users to add posts and i want to insert the user id dynamicaly from asession into db
when a user send anew post ?
any help ?
Note i'm using Tank auth so i can use get_user_id method to know the user id but my question is how to send this id while sending a post ?
Posted 08 September 2012 - 00:11 AM
Posted 08 September 2012 - 00:17 AM
Posted 08 September 2012 - 00:21 AM
function add_comment($module,$post_id, $user_id, $text)
{
$data = array(
'module' => $module ,
'post_id' => (int)$post_id ,
'user_id' => (int)$user_id,
'text' =>$text,
);
$this->db->insert('comments', $data);
$sql = "UPDATE `users` SET `count_comments` =`count_comments`+1 WHERE `id`='".$user_id."'";
$this->db->query($sql);
return TRUE;
}
controller:
if($data['article']['data']['commentable'] == "Y")
{
if($this->_USER_DATA['id'])
{
$this->form_validation->set_rules('text', 'comment', 'trim|required|min_length[1]|max_length[10000]|xss_clean|htmlspecialchars');
$this->form_validation->set_rules('post_id', 'post_id.', 'trim|required|min_length[1]|max_length[10]|xss_clean|is_natural');
if ($this->form_validation->run())
{
$this->comments->add_comment(__CLASS__,$this->input->post('post_id'), $this->_USER_DATA['id'], $this->input->post('text'));
redirect($this->uri->uri_string());
}
}
}
Helped you?
P.S: I have use other library,but the meaning is the same.
Posted 08 September 2012 - 02:48 AM
i'm a new learner to codeigniter and gcrud too!
i'm using this code to allow users to add posts on my site
function new_post()
{
$crud = new grocery_CRUD();
$crud->set_table('articles');
$crud->columns('article_title');
$crud->fields('article_title','[color=#ff0000][b]user_id[/b][/color]');
$data = $crud->render();
}
so i want to set [color=#ff0000][b]user_id[/b][/color] value to $this->tank_auth_get_user_id();
how can i do this ?
and how to hide this input field 'user_id' from adding form ?
thank you for your answer
Posted 08 September 2012 - 08:24 AM
function new_post(){
...
$crud->callback_add_field(user_id,array($this,'user_id'));
...
}
function user_id()4
{
return '<input type="hidden" value="'.$this->session->userdata('user_id');.'" name="user_id" >';
}
but I would not do it, because it can be faked "user_id"[/s]
Posted 08 September 2012 - 09:01 AM
Posted 23 September 2012 - 14:04 PM
use function callback_before_insert. http://www.grocerycr...k_before_insert
[/quote]
Thanks Victor