[英]How to update and delete items from a sql database using codeigniter?
I wanted to be able to update and delete items for a list of products from an sql database. 我希望能够从sql数据库中更新和删除产品列表中的项目。 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 Product.php-控制器
<?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 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 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>
What you are making is called CRUD - create, read, update, delete. 您所做的称为CRUD-创建,读取,更新,删除。 There are a lot of examples of doing CRUD with CodeIgniter in Google .
在Google中,有很多使用CodeIgniter进行CRUD的示例。
Below are (over)simplified conceptual samples. 以下是(过度)简化的概念样本。
Based on your HTML, adding links to edit and delete a product: 根据您的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>
And in Product.php - controller we'll add new methods to edit and delete 在Product.php-控制器中,我们将添加新的方法来进行编辑和删除
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 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 product_model.php
public function update_product($id, $pdata)
{
$this->db->where("product_id", $id);
$this->db->update("product", $pdata);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.