如何使用codeigniter更新和删除sql数据库中的项目?

时间:2015-12-11 18:52:30

标签: php mysql codeigniter

我希望能够从sql数据库更新和删除产品列表中的项目。我已经列出了数据库中的表格,我可以阅读并插入新产品,但我不确定如何更新产品或删除它?任何建议或教程都会很好吗?我尝试了一些,但似乎没有工作。 这是我展示产品和插入新产品的代码

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 - 查看

  <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 个答案:

答案 0 :(得分:1)

您正在制作的内容称为CRUD - 创建,读取,更新,删除。有很多doing CRUD with CodeIgniter in Google的例子。

以下是(简化)简化概念样本。

根据您的HTML,添加编辑和删除产品的链接:

<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> 

在Product.php - controller中,我们将添加新方法来编辑和删除

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 - 查看

<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);
}