Welcome Guest, Not a member yet? Register   Sign In
Inserting into table twice..:(
#11

[eluser]InsiteFX[/eluser]
Whats wrong with installing XDEBUG?

InsiteFX
#12

[eluser]InsiteFX[/eluser]
Well here is one problem I see!
Code:
function add_member($id,$member)
    {
        $data = array(
                       'member_id' => $member,
                       'friend' => $id,
                       'status' => '0' ''  //  <-- Remove the last 2 single quotes!                    
                    );
        $this->db->insert('friends',$data);
    }

InsiteFX
#13

[eluser]Roy MJ[/eluser]
No the '' is not there. It somehow got in while i pasted it here.
The model part :

Code:
function add_member($id,$member)
    {
        $data = array(
                   'member_id' => $member,
                   'friend' => $id,
                   'status' =>  '0'                      
            );
        $this->db->insert('friends',$data);
    }

Both $member and $id are being sent from the controller. First time its adding with both $member and $id values. Then it adds one more time, this time with $id value as 0. Im going nuts...
#14

[eluser]WanWizard[/eluser]
How is your controller method 'memberadd' called?
#15

[eluser]InsiteFX[/eluser]
As WanWizard stated show your Controller Code!

There is a reason why it is inserting twice!

InsiteFX
#16

[eluser]Roy MJ[/eluser]
Here is the whole controller :

Code:
class Members extends Controller {
    var $data    =    array();    
    function Members(){
        parent::Controller();    
        $this->load->model('Members_model');
        $this->load->model('Profile_model');
        $this->site_settings->get_site_settings();
        $this->load->helper(array('form'));
        $this->load->library(array('form_validation','pagination'));
        $this->get_common();
    }
    
    function index($pgoffset=''){
        $config['per_page'] = $this->config->item('records_per_page');
        $config['total_rows'] = $this->Members_model->get_total();
        $config['base_url'] = base_url().'index.php/members/index/';
        $config['uri_segment'] = 3;
        $row    =      $this->Profile_model->get_selected($this->session->userdata('user_id'));
        $this->data['username'] = $row->username;
        $data['count_no'] = ($this->uri->segment(3)=='')? 0 : $this->uri->segment(3);
        $data['members']=  $this->Members_model->get_all($config['per_page'], $pgoffset);
        $data['friends']=  $this->Members_model->get_friends();
        $data['pgoffset'] = $pgoffset;
        
        $this->pagination->initialize($config);
        $this->load->view('members/list', $data);
    }
    function search($pgoffset='')
    {
        $tags = $this->input->post('tags');
        $config['per_page'] = $this->config->item('records_per_page');
        $config['total_rows'] = $this->Members_model->get_friends_total($tags);
        $config['base_url'] = base_url().'index.php/members/search/'.$tags.'/';
        $config['uri_segment'] = 4;

        $this->data['count_no'] = ($this->uri->segment(4)=='')? 0 : $this->uri->segment(4);
        $this->data['frnds']=  $this->Members_model->get_friends_all($tags,$config['per_page'], $pgoffset);
        
        $this->data['pgoffset'] = $pgoffset;
        $this->pagination->initialize($config);
        $this->load->view('members/listfriends', $this->data);
        }
        
    function memberadd($id, $pgoffset='')
    {
        $this->Members_model->add_member($id,$member=$this->session->userdata('user_id'));
        /*$row    =      $this->Profile_model->get_selected($this->session->userdata('user_id'));
        $this->data['username'] = $row->username;
        
        $config['per_page'] = $this->config->item('records_per_page');
        $config['total_rows'] = $this->Members_model->get_total_friends();
        $config['base_url'] = base_url().'index.php/members/index/';
        $config['uri_segment'] = 3;
        $row    =      $this->Profile_model->get_selected($this->session->userdata('user_id'));
        $this->data['username'] = $row->username;
        $data['count_no'] = ($this->uri->segment(3)=='')? 0 : $this->uri->segment(3);
        $data['members']=  $this->Members_model->get_all_friends($config['per_page'], $pgoffset);*/
        /*$data['pgoffset'] = $pgoffset;*/
        $this->load->view('members/listfriends', $this->data);
    }
        
    function get_common(){
        $this->site_settings->get_site_settings();
    }
    }
#17

[eluser]Roy MJ[/eluser]
Out of desperation ive found a way past this. Not ethical but i think it does the purpose. I made a small change to the model :

Code:
function add_member($id,$member)
    {
        $data = array(
                   'member_id' => $member,
                   'friend' => $id,
                   'status' =>  '0'                      
                );
        $this->db->insert('friends',$data);
        [b]$this->db->delete('friends', array('friend' => 0));[/b] :)
    }
#18

[eluser]WanWizard[/eluser]
So, what happens if you type "http://yourwebsite/members/memberadd" in your browser?
It would just call your models add_member() method without a valid $id and with an empty $_POST array.

So again, check for multiple calls, possibly due to rewrite issues. The first request will be the post of the form, the second one an HTTP GET which will do the same as typing the URL in the browser.

Your logic is flawed. If you're going to process form data, make sure there's a form posted, and display an error message and/or redirect to the form if not.
#19

[eluser]Roy MJ[/eluser]
Yeah now ive included a session check whether the member is logged in or not, else he'l be redirected to login page. I din understand about form and all. All i need is to pass a value through a link. So i thought of passing it through <a href=''></a> tags.

I have also changed the config file to $config['log_threshold'] = 4;

No errors are showing..
#20

[eluser]toopay[/eluser]
According to your approach, i suggest more proper (and readable) structure, like this :
Code:
class Members extends Controller {
    protected $_data
    function __construct()
    {
        parent::__construct();    
        $this->load->model('Members_model');
        $this->load->model('Profile_model');
        $this->load->helper(array('form'));
        $this->load->library(array('form_validation','pagination'));
        $this->site_settings->get_site_settings();
        $this->get_common();
        $this->_data = array();
    }
    
    function index($pgoffset='')
    {
        $config = $this->set_config($this->config->item('records_per_page'),$this->config->item('records_per_page'),base_url().'index.php/members/index/',3);
        $this->pagination->initialize($config);

        $this->_data['username'] = $this->Profile_model->get_selected($this->session->userdata('user_id'))->username;
        $data = array(
                'count_no' => ($this->uri->segment(3)=='')? 0 : $this->uri->segment(3),
                'members'  => $this->Members_model->get_all($config['per_page'], $pgoffset),
                'friends'  => $this->Members_model->get_friends(),
                'pgoffset' => $pgoffset,    
        );

        $this->load->view('members/list', $data);
    }

    function search($pgoffset='')
    {
        $tags = $this->input->post('tags');
        $config = $this->set_config($this->config->item('records_per_page'),$this->Members_model->get_friends_total($tags),base_url().'index.php/members/search/'.$tags.'/',4);
        $this->pagination->initialize($config);

        $this->_data = array(
                'count_no' =>  ($this->uri->segment(4)=='')? 0 : $this->uri->segment(4),
                'frnds'    => $this->Members_model->get_friends_all($tags,$config['per_page']),
                'pgoffset' => $pgoffset,
        );
        $this->load->view('members/listfriends', $this->data);
    }
        
    function memberadd($id, $pgoffset='')
    {
        $this->Members_model->add_member($id,$member=$this->session->userdata('user_id'));
        /*$row    =      $this->Profile_model->get_selected($this->session->userdata('user_id'));
        $this->data['username'] = $row->username;
        
        $config['per_page'] = $this->config->item('records_per_page');
        $config['total_rows'] = $this->Members_model->get_total_friends();
        $config['base_url'] = base_url().'index.php/members/index/';
        $config['uri_segment'] = 3;
        $row    =      $this->Profile_model->get_selected($this->session->userdata('user_id'));
        $this->data['username'] = $row->username;
        $data['count_no'] = ($this->uri->segment(3)=='')? 0 : $this->uri->segment(3);
        $data['members']=  $this->Members_model->get_all_friends($config['per_page'], $pgoffset);*/
        /*$data['pgoffset'] = $pgoffset;*/
        $this->load->view('members/listfriends', $this->data);
    }
        
    function get_common(){
        $this->site_settings->get_site_settings();
    }

    function set_config($per_page, $total_rows, $base_url, $uri_segment)
    {
        $config = array(
                'per_page'      => $per_page,
                'total_rows'    => $total_rows,
                'base_url'      => $base_url,
                'uri_segment'   => $uri_segment,
        );
        return $config;
    }
}




Theme © iAndrew 2016 - Forum software by © MyBB