Welcome Guest, Not a member yet? Register   Sign In
insert problem
#1

[eluser]dianikol85[/eluser]
Hi to all.

i have the following. A short version of the actual code but it will give you the idea

Code:
class my_controller extends CI_Controller {
    
    private $message = '';

    function  index() {
         $data['message'] = $message;

         $this->load->view('my_view',$data);
    }


    function  add_to_the db() {
         $this->load->model(mytable_model);
         $this->mytable_model->do_the_insert();
        
         $message = 'success message';

         $this->index();
    }
    
}

So far so good. But when the $this->index(); is called it two rows in the db instead of one

if i use redirect('my_controller/index') or if commenting out this line
Code:
//$this->index();
it adds one row as it should.

Does it happen to anyone else???

I dont want to use the redirect function beacause i also i pass a message to the index that the insert is success.
#2

[eluser]Cristian Gilè[/eluser]
$message doesn't exist. It should be
Code:
$this->message

The right code:

Code:
class my_controller extends CI_Controller {
    
    private $message = '';

    function  index() {
         $data['message'] = $this->message;

         $this->load->view('my_view',$data);
    }


    function  add_to_the db() {
         $this->load->model(mytable_model);
         $this->mytable_model->do_the_insert();
        
         $this->message = 'success message';

         $this->index();
    }
    
}

If you are not creating a base controller, change the controller name to something else. MY_ is a private keyword for codeigniter.


Cristian Gilè
#3

[eluser]dianikol85[/eluser]
i dont use MY_. i used it only for this example. I also hav $this->maessage, I misspellit when i wrote this post. The code is correct anyway. Any thoughts about this double insertation??
#4

[eluser]AlunR[/eluser]
[quote author="dianikol85" date="1298931397"]i dont use MY_. i used it only for this example. I also hav $this->maessage, I misspellit when i wrote this post. The code is correct anyway. Any thoughts about this double insertation??[/quote]

Put the message in a SESSION Flash and call it back from there.

No idea why it's running twice. I imagine there is a deeper problem somewhere else in the code.
#5

[eluser]Cristian Gilè[/eluser]
Please, post the model code.


Cristian Gilè
#6

[eluser]dianikol85[/eluser]
The model is

Code:
function user_insert($user_array)
    {
        if ($this->db->insert('user', $user_array))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }


The method where i do the post request is

Code:
function user_added()
    {

        // Retrieve the date from edit_user page
        $u_username = $this->input->post('u_username');
        $u_fname = $this->input->post('u_fname');
        $u_lname = $this->input->post('u_lname');
        $u_address = $this->input->post('u_address');
        $u_email = $this->input->post('u_email');
        $u_phone = $this->input->post('u_phone');
        $u_mobile = $this->input->post('u_mobile');
        $u_publish = $this->input->post('u_publish');
        $u_group = $this->input->post('u_group');
        $u_password = $this->input->post('u_password');

      
            $user_array = array(
                'u_username' => $u_username,
                'u_password' => sha1($u_password),
                'u_fname' => $u_fname,
                'u_lname' => $u_lname,
                'u_address' => $u_address,
                'u_email' => $u_email,
                'u_date_added' => date('Y-m-d'),
                'u_phone' => $u_phone,
                'u_mobile' => $u_mobile,
                'u_group' => $u_group,
                'u_publish' => $u_publish
            );

            // Send the data to the user model to insert the user
            if ($this->user_model->user_insert($user_array))
            {
                $this->notification_type = 'alert_success';
                $this->notification_msg = 'The user has been added successfully';
                $this->index();
            }
            else
            {
                $this->notification_type = 'alert_error';
                $this->notification_msg = 'The user has faild to add';
                $this->index();
            }
      

    }

The index method is

Code:
function index()
    {

            $data['users'] = $this->user_model->get_users($config['per_page'],$offset,$order_by,$sort_order);
        

        // Set the data array to pass it to the view
        $data['main_content'] = 'admin/user/user_list';
        $data['title'] = 'Διαχείριση χρηστών';
        $data['username'] = $this->session->userdata('username');
        

        // Check if thera has been a crud action by checking the $notification_type,$notification_msg
        if (strlen($this->notification_type) > 0)
        {
            $data['notification_type'] = $this->notification_type;
            $data['notification_msg'] = $this->notification_msg;
        }

        // Load the view and pass to it the $data array
        $this->load->view('admin/template/template',$data);
    }
#7

[eluser]Cristian Gilè[/eluser]
I can't see any error in the code you provided.

Hint: When you're doing "write" type queries (insert, update, etc.) you should use $this->db->affected_rows() to know the number of affected rows.

Code:
function user_insert($user_array)
{
    $this->db->insert('user', $user_array));
    if($this->db->affected_rows() > 0)
        return TRUE;
    return FALSE;
}


Cristian Gilè
#8

[eluser]dianikol85[/eluser]
Thanks for tip, I'll use it from now on.

Do you suggest to use session to store the message and then use redirect(). As i said before if i use this it works fine
#9

[eluser]Cristian Gilè[/eluser]
In the user_added() method, what's after this piece of code ?

Code:
// Send the data to the user model to insert the user
            if ($this->user_model->user_insert($user_array))
            {
                $this->notification_type = 'alert_success';
                $this->notification_msg = 'The user has been added successfully';
                $this->index();
            }
            else
            {
                $this->notification_type = 'alert_error';
                $this->notification_msg = 'The user has faild to add';
                $this->index();
            }


Is there something else ?


Cristian Gilè
#10

[eluser]dianikol85[/eluser]
Nothing else. Just that. This is thecode the user_added contains.

if i comment out this line $this->index() the query will run once as it should be.

I can't see any mistakes in index() method. This is pretty frustrating Sad




Theme © iAndrew 2016 - Forum software by © MyBB