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

标签 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");
   }
   }
   }
   ?>

产品模型.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>

最佳答案

您所做的称为 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>

产品模型.php

public function update_product($id, $pdata)
{
    $this->db->where("product_id", $id);
    $this->db->update("product", $pdata);
}

关于php - 如何使用 codeigniter 更新和删除 SQL 数据库中的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34230579/

相关文章:

php - 插入前检查日期时间字段

mysql - SQL - 检索过去 7 天的最后一天记录

PHP数组到json可读输出

php - mysql在where条件下获取数据

PHP:多维数组上的 array_push 并显示其元素

php - 导入excel到mysql报错

php - 如何从数据库codeigniter中扣除值

php - 更新已执行PHP的表

PHP isset 不适用于复选框

php - 在php文件中包含一个网站