0

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>

1 Answer 1

1

What you are making is called CRUD - create, read, update, delete. There are a lot of examples of doing CRUD with CodeIgniter in Google.

Below are (over)simplified conceptual samples.

Based on your HTML, adding links to edit and delete a product:

<td width="40" align="left">
    <a href="<?php echo base_url();?>/index.php/product/edit/<?php echo $p_key->id;?>">Edit</a>

    <!-- Don't really delete like this, seciruty issue! Just shown as a concept for manipulating records with IDs -->
    <a href="<?php echo base_url();?>/index.php/product/delete/<?php echo $p_key->id;?>">Delete</a>
</td> 

And in Product.php - controller we'll add new methods to edit and delete

public function edit($id)
{
    if($_SERVER['REQUEST_METHOD'] == "POST")
    {
       // get fields from form and update
       // database record with them
       $pdata['Name'] = $this->input->post('Name');
       $pdata['Type'] = $this->input->post('Type');
       $pdata['Price'] = $this->input->post('Price');

       $this->product_model->update_product($id, $pdata);        
    }
    else
    {
        // show form to edit product
        // need to get product from database and pass to a view
        $product = $this->product_model->getproduct_by_id($id);

        $this->load->view('header');
        $this->load->view('nav');    
        $this->load->view('edit', array("product" => $product));
        $this->load->view('footer');        
    }
}

public function delete($id)
{
     $this->product_model->delete($id);
}

edit.php - View

<form action="/index.php/product/edit/<?php echo $product->id ?>" method="post">
<table>
<tr>
    <td><input name="name" value="<?php echo $product->Name; ?>"></td>
</tr>
<tr>
    <td><input name="type" value="<?php echo $product->Type; ?>"></td>
</tr>
<tr>
    <td><input name="price" value="<?php echo $product->Price; ?>"></td>
</tr>
<tr>
    <td>
        <input type="submit" value="Update">
    </td>
</tr>
</table>
</form>

product_model.php

public function update_product($id, $pdata)
{
    $this->db->where("product_id", $id);
    $this->db->update("product", $pdata);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.