我有一些包含编辑和删除选项的产品的表格视图。当我点击编辑时,它会重定向到另一个页面并显示特定数据。有一个名为产品类别的字段显示产品类别组合框中的产品,例如比萨饼。
现在,问题开始了。假设我从组合框中选择Sandwich而不是Pizza,并尝试在数据库中更新该值。它不会更新任何内容。
我已经将Codeigniter用于这个项目,所以我想要同样的解决方案。我也在谷歌搜索过其他帮助,但运气不错。我没有得到适当的解决方案。
任何回复都会受到赞赏。
我的代码片段如下所示。
1)控制器
function products_edit($product_id)
{
$this->load->library('form_validation');
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('products_model');
$data=$this->products_model->general();
$product = $this->products_model->get_product($product_id);
$category['categories']=$this->products_model->get_category();
$this->data1['title'] = 'Edit Product';
//validate form input
$this->form_validation->set_rules('name', 'Product name', 'required|xss_clean');
$this->form_validation->set_rules('description', 'Description', 'required|xss_clean');
//$this->form_validation->set_rules('category', 'Category', 'required|xss_clean');
//$this->form_validation->set_rules('extras', 'Extras', 'required|xss_clean');
$this->form_validation->set_rules('price', 'Price', 'required|xss_clean');
$this->form_validation->set_rules('is_featured', 'Is Featured', 'required|xss_clean');
$this->form_validation->set_rules('prorder', 'Order', 'required|xss_clean');
if (isset($_POST) && !empty($_POST))
{
$data1 = array(
'product_name'=> $this->input->post('name'),
'product_desc'=> $this->input->post('description'),
'product_category'=> $this->input->post('category'),
'extras'=> $this->input->post('extras'),
'price'=> $this->input->post('price'),
'is_featured'=> $this->input->post('is_featured'),
'prorder' => $this->input->post('order'),
);
if ($this->form_validation->run() === true)
{
$this->products_model->updateproducts($product_id, $data1);
$this->session->set_flashdata('message', "<p>Product updated successfully.</p>");
redirect('products_controller/products_edit/'.$product_id);
}
}
$this->data1['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
$this->data1['product'] = $product;
//$this->data1['category']=$category;
//display the edit product form
$this->data1['name'] = array(
'name' => 'name',
'id' => 'name',
'type' => 'text',
'style' => 'width:300px;',
'value' => $this->form_validation->set_value('name', $product['product_name']),
);
$this->data1['description'] = array(
'name' => 'description',
'id' => 'description',
'type' => 'text',
'cols' => 40,
'rows' => 5,
'value' => $this->form_validation->set_value('description', $product['product_desc']),
);
$category1=$this->products_model->get_category();
$category1['value']=set_value('category',$product['product_category']);
$temp=array(
'1'=>$category1['value'],
);
$category1=array_diff($category1,$temp);
$category1['value']=set_value('category',$product['product_category']);
$this->data1['category']=$category1;
//$this->data1['category'] = array(
//'name' => 'category',
// 'id' => 'category',
// 'type' => 'text',
// 'style' => 'width:300px;',
//'value' => $this->form_validation->set_value('category',$category['categories']),set_value('category',$product['product_category']),
//$this->data1['category']= $this->form_validation->set_value('category',$category['categories']);
//);
$this->data1['extras'] = array(
//'name' => 'extras',
//'id' => 'extras',
//'type' => 'text',
//'style' => 'width:300px;',
'value' => $this->form_validation->set_value('extras', $product['extras']),
);
$this->data1['price'] = array(
'name' => 'price',
'id' => 'price',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('price', $product['price']),
);
$this->data1['is_featured'] = array(
'name' => 'is_featured',
'id' => 'is_featured',
'type' => 'text',
'style' => 'width:40px;text-align: right',
'value' => $this->form_validation->set_value('isfeatured', $product['is_featured']),
);
$this->data1['prorder'] = array(
'name' => 'prorder',
'id' => 'prorder',
'type' => 'text',
'style' => 'width:40px;',
'value' => $this->form_validation->set_value('prorder', $product['prorder']),
);
$this->load->view('products_edit', $this->data1);
}
2)模型
function get_product($product_id) {
$this->db->select('product_id,product_name,product_desc,product_category,extras,price,is_featured,prorder');
$this->db->where('product_id', $product_id);
$this->db->distinct('product_category');
$query = $this->db->get('product');
return $query->row_array();
}
function updateproducts($product_id, $data)
{
$this->db->where('product_id', $product_id);
$this->db->update('product', $data);
}
第3)查看
<?php $product_id = $product['product_id']; ?>
<?php echo form_open("products_controller/products_edit/$product_id");?>
<table width="500" border="1" cellpadding="0" cellspacing="2" align="center">
<tr>
<td width="130" align="right"> Product Name: </td>
<td><?php echo form_input($name); ?></td>
</tr>
<tr>
<td width="130" align="right"> Product Description: </td>
<td><?php echo form_textarea($description); ?></td>
</tr>
<tr>
<td align="right">Product Category:</td>
<td>
<?php echo form_dropdown("product_id",$category,'value')?>
</td>
</tr>
<tr>
<td align="right">Extras:</td>
<td><?php echo form_dropdown("product_id",$extras,"#","id='product_id'");?></td>
</tr>
<tr>
<td align="right">Price:</td>
<td><?php echo form_input($price); ?></td>
</tr>
<tr>
<td align="right">Is Featured:</td>
<td><?php echo form_input($is_featured); ?></td>
</tr>
<tr>
<td align="right">Order:</td>
<td><?php echo form_input($prorder); ?></td>
</tr>
<tr>
<td> </td>
<td><?php echo form_submit('submit', 'Submit');?>
</td>
</tr>
</table>
<table align="center">
<tr>
<td>
<?php echo form_close(); ?>
<?php echo form_open("products_controller/products_search");?>
<?php echo form_submit('submit', 'Back');?>
<?php echo form_close(); ?>
提前致谢。
答案 0 :(得分:2)
使用下面的代码,
$test = array();
$count=count($category['categories']);
for($i=0;$i<$count;$i++)
{
$test[$i] = array($category['categories'][$i] => $category['categories'][$i]);
}
$this->data['category']=set_value('category',$test);
答案 1 :(得分:0)
获取<?php echo form_dropdown("product_id",$category,'value')?>
的值
你必须写$this->input->post('product_id')
这将获得产品ID,然后你可以用它来更新db