Welcome Guest, Not a member yet? Register   Sign In
returning affected rows
#1

[eluser]Unknown[/eluser]
Hi Everyone - I am totally new to codeignitor and fairly new to php so please excuse my rookie question.

I am adding a record to a database and want to return load a different view if the addition to the database has been successful. I decided that using the affected_rows() function might be a good way to do this.

this is my controller code
Code:
function insert_product(){
        $this->load->model('admin_model');
        if($this->input->post('submit')){
         $companies_id = $this->input->xss_clean($this->input->post('companies_id'));
         $product_description = $this->input->xss_clean($this->input->post('product_description'));
         $price = $this->input->xss_clean($this->input->post('price'));
         $products_comments = $this->input->xss_clean($this->input->post('products_comments'));
        
        $this->admin_model->add_product($product_description, $price, $products_comments);
        }
       $this->load->view('insert_product', $data);
       }]

my model code is as follows.

Code:
function add_product ($companies_id, $product_description, $price, $product_comments){
        $data = array(
            'companies_id' => $companies_id,
            'product_description' => $product_description,
            'price' => $price,
            'product_comments' => $product_comments
            );
    $this->db->insert('products', $data);

I have tried every way that I can think of the return the effected rows to the controller, however my variable never seems to contain any data.

Can anyone shead any light on the best way to do this.

Many thanks
#2

[eluser]tonanbarbarian[/eluser]
well firstly the code is only ever going to effect either 1 row or none. the insert will only create a single row or none

try using the insert_id to determine the id of the new record
Code:
$this->db->insert_id();
if it returns false then the insert failed
#3

[eluser]Unknown[/eluser]
Hi

Many thanks for your reply. That makes logical sense. I will try and create something around that function.

how would I go about loading a different view if it returned true to if it returned false?




Theme © iAndrew 2016 - Forum software by © MyBB