I wanted to be able to update and delete items for a list of products from an sql database. I already have the table from a database listed and I am able to read and insert a new product but I'm not sure how to update the product or delete it? Any advice or tutorials would be good? I have tried a few but none seem to work. Here is my code for showing the products and inserting a new product
Product.php - controller
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
class Product extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('product_model');
}
public function index()
{
$data['product_list'] = $this->product_model->getproduct();
$this->load->view('header');
$this->load->view('nav');
$this->load->view('product', $data);
$this->load->view('footer');
}
public function add_form()
{
$this->load->view('header');
$this->load->view('nav');
$this->load->view('insert');
$this->load->view('footer');
}
public function insert_product()
{
$pdata['Name'] = $this->input->post('Name');
$pdata['Type'] = $this->input->post('Type');
$pdata['Price'] = $this->input->post('Price');
$res = $this->product_model->insert_product($pdata);
if($res){
header('location:'.base_url()."index.php/Product/index");
}
}
}
?>
Product_model.php
<?php
class Product_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function getproduct()
{
$query = $this->db->get('testProduct');
return $query->result();
}
public function insert_product($data)
{
return $this->db->insert('testProduct', $data);
}
}
?>
product.php - View
<h2> Product</h2>
<table width="600" border="1" cellpadding="5">
<tr>
<th scope="col">Id</th>
<th scope="col">Name</th>
<th scope="col">Type</th>
<th scope="col">Price</th>
</tr>
<?php foreach ($product_list as $p_key){ ?>
<tr>
<td><?php echo $p_key->id; ?></td>
<td><?php echo $p_key->Name; ?></td>
<td><?php echo $p_key->Type; ?></td>
<td><?php echo $p_key->Price; ?></td>
<td width="40" align="left" ><a href="#" <?php echo $p_key->id;?> >Edit</a></td>
<td width="40" align="left" ><a href="#" <?php echo $p_key->id;?>>Delete </a></td>
</tr>
<?php }?>
<tr>
<td colspan="7" align="right"> <a href="<?php echo base_url();? >index.php/Product/add_form">Insert New Product</a></td>
</tr>
</table>