Welcome Guest, Not a member yet? Register   Sign In
Newb question on updating record in MySQL
#1

[eluser]symonel[/eluser]
Hi
This is gonna sound rather silly, but I get this weird error when I try to update a record in my 'products' table with the fields: product_id (int), product_name (varchar), product_description (varchar), product_price (int), seller_id (int). So here's my code:
This is my model function for getting a product:
Code:
function get_product($product_id)
    {
            $this->db->select('*');
            $this->db->from('products');
            $this->db->join('users', 'users.id = products.seller_id');
            $this->db->where('product_id', $product_id);

            $query = $this->db->get();

            return $query;
    }
and for updating a product:
Code:
function update_product($product, $id)
    {
        $this->db->where('product_id', $id);
        $this->db->update('products', $product);
        return $this->db->insert_id();
    }
This is my controller:
Code:
function update_product($id = false)
    {
        if ($this->session->userdata('logged_in')) {
            $data['user']['logged_in'] = TRUE;
        } else {
            $data['user']['logged_in'] = FALSE;
            redirect('user/login');
        }
        
        $id_produs = $this->product_model->get_product($id);
                
        $fields['product_name'] = 'product_name';
        $fields['product_description'] = 'product_description';
        $fields['product_price'] = 'product_price';
        
        $produs = array(
                'product_name' => $this->input->post('product_name', TRUE),
                'product_description' => $this->input->post('product_description', TRUE),
                'product_price' => $this->input->post('product_price', TRUE),
                );
                
        $data['single_product'] = $this->product_model->update_product($produs, $id_produs);
        $this->load->view('product_view', $data);
        
    }
And this is form in my view:
Code:
<?= form_open('bsc/update_product'); ?>
<p><label for="product_name">Product name:</label><br />
&lt;input type="text" name="product_name" id="product_name" /&gt;</p>
<p><label for="product_description">Product description:</label><br />
&lt;input type="text" name="product_description" id="product_description" /&gt;</p>
<p><label for="product_price">Product price:</label><br />
&lt;input type="text" name="product_price" id="product_price" /&gt;</p>
<p>&lt;input type="submit" value="Update Product" /&gt;</p>                
&lt;?= form_close(); ?&gt;

Quote:And what I get is this:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: database/DB_active_rec.php
Line Number: 450

Quote:An Error Was Encountered
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
UPDATE `products` SET `product_name` = 'Notebook HP 530', `product_description` = 'Intel Core Duo T2600 2.16GHz, RAM 1GB, 120GB', `product_price` = '250' WHERE `product_id` =

And that's it. No idea why this happens. Or why there doesn't seem to be a value for the id. Please help.
#2

[eluser]Seppo[/eluser]
get_product is returning a result object, and you are using it as an id... so, you might have to do

Code:
function update_product($id = false)
    {
        if ($this->session->userdata('logged_in')) {
            $data['user']['logged_in'] = TRUE;
        } else {
            $data['user']['logged_in'] = FALSE;
            redirect('user/login');
        }
        
        $id_produs = $this->product_model->get_product($id);
                
        $fields['product_name'] = 'product_name';
        $fields['product_description'] = 'product_description';
        $fields['product_price'] = 'product_price';
        
        $produs = array(
                'product_name' => $this->input->post('product_name', TRUE),
                'product_description' => $this->input->post('product_description', TRUE),
                'product_price' => $this->input->post('product_price', TRUE),
                );
                
        $data['single_product'] = $this->product_model->update_product($produs, $id);
        $this->load->view('product_view', $data);
        
    }
#3

[eluser]symonel[/eluser]
Eventually I added a hidden field with the product_id inside the form in the view and used it in the controller and it worked. Thanks anyway!




Theme © iAndrew 2016 - Forum software by © MyBB