[eluser]sith4life[/eluser]
the following checks to see if the update button is pressed
Code:
if($this->input->post('update'))
{
$this->Products_model->update_cart($this->input->post('cart_id'),$this->input->post('qty'));
}
if so run this query
Code:
function update_cart($id,$qty,$delete = false)
{
if($delete === false)
{
$query = '
UPDATE carts
SET carts.qty = "'.$qty.'"
WHERE carts.id = "'.$id.'"
';
}
else
{
$query = '
DELETE FROM carts
WHERE carts.id = "'.$id.'"
';
}
$this->db->query($query);
}
EXCEPT. $id is being populated with the products.id (or carts.product_id) and not the carts.id field so the query just returns with no rows affected.
$id is passed from a hidden field with the value set to what is supposed to be the correct ID, but it's not.
Code:
echo form_open('store/cart');
$options = array('name' => 'qty','value' => $cart->qty, 'maxlength' => '3', 'size' => '2');
echo form_input($options);
echo form_hidden('cart_id',$cart->id);
echo form_submit('update','Update Quantity');
echo form_close();
Here is the ENTIRE Products_model.php file
Code:
<?php
class Products_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function get_all_products()
{
$query = $this->db->query('SELECT * FROM products');
return $query->result();
}
function add_to_cart($info)
{
$result = $this->db->insert('carts',$info);
if($result)
{
return true;
}
else
{
return false;
}
}
function view_cart($email)
{
$query = $this->db->query('SELECT carts.*, products.* FROM carts, products WHERE carts.product_id = products.id');
return $query->result();
}
/*
* takes 2 parameters id, qty and 1 optional
* boolean, delete
*
* if delete is set the item is deleted from the table
* otherwise update the qty field for the select cart item
*
*
*/
function update_cart($id,$qty,$delete = false)
{
if($delete === false)
{
$query = '
UPDATE carts
SET carts.qty = "'.$qty.'"
WHERE carts.id = "'.$id.'"
';
}
else
{
$query = '
DELETE FROM carts
WHERE carts.id = "'.$id.'"
';
}
$this->db->query($query);
}
function add_product($info)
{
$result = $this->db->insert('products',$info);
if($result)
{
return true;
}
else
{
return false;
}
}
}