更新产品表时遇到此问题。 它显示错误消息但我无法确定问题的位置,因为它的自定义消息。 我只需要在用户进行更改时更新产品,并通知用户是否进行了更改。
product_update_view.php
<div class="col-sm-10" id="main" style="padding: 20px;">
<?php
echo form_open("item/item_update/$item->prod_id");
echo form_fieldset('<h3 class="text-info">Update Item</h3>');
?>
<input type="hidden" name="cur_code" value="<?php echo $item->prod_code ?>">
<input type="hidden" name="cur_name" value="<?php echo $item->prod_desc ?>">
<input type="hidden" name="cur_category" value="<?php echo $item->category_name ?>">
<input type="hidden" name="cur_brand" value="<?php echo $item->brand_name ?>">
<input type="hidden" name="cur_sizes" value="<?php echo $item->size ?>">
<input type="hidden" name="cur_price" value="<?php echo $item->original_price ?>">
<div class="form-group">
<label for='prod_code'>Product Code:</label>
<input type="text" name="up_code" class="form-control" value="<?php echo $item->prod_code; ?>">
</div>
<div class="form-group">
<label for='prod_name'>Product Description:</label>
<input type="text" name=up_name" class="form-control" value="<?php echo $item->prod_desc; ?>">
</div>
<div class="form-group">
<label for='up_brand'>Brand:</label>
<select name="up_brand" class="form-control">
<option value="Select Brand" selected="selected">Select Brand</option>
<?php foreach($brand as $br): ?>
<option value="<?php echo $br->brand_id; ?>" <?php
if($br->brand_id == $item->brand_id)
{
echo "selected='selected'";
}
?>><?php echo $br->brand_name ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="category">Category</label>
<select class="form-control" name="up_category" id="category">
<option value="Select Brand"> Select Category</option>
<?php foreach($category as $cat): ?>
<option value="<?php echo $cat->category_id; ?>" <?php
if($cat->category_id == $item->category_id)
{
echo "selected='selected'";
}
?>><?php echo $cat->category_name; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for="sizes">Size:</label>
<select class="form-control" name="up_sizes" id="sizes" disabled="disabled">
<?php foreach($sizes as $sz): ?>
<option value="<?php echo $sz->size_id; ?>" <?php
if($sz->size_id == $item->size_id)
{
echo "selected='selected'";
}
?>><?php echo $sz->size; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label for='prod_price'>Price:</label>
<input type="text" name="up_price" class="form-control" value="<?php echo $item->original_price; ?>">
</div>
<div class="form-group">
<input type="submit" name="submit_account" class="btn btn-primary" value="Update Product" >
</div>
</div>
<script src="<?php echo base_url() ?>dropdown/js/jquery.js"></script>
<script src="<?php echo base_url() ?>dropdown/js/bootstrap.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#category').on('click', function () {
var category_id = $(this).val();
if (category_id === '')
{
$('#sizes').prop('disabled', true);
} else
{
$('#sizes').prop('disabled', false);
$.ajax({
url: "<?php echo base_url() ?>welcome/get_sizes",
type: "POST",
data: {'category_id': category_id},
dataType: 'json',
success: function (data) {
$('#sizes').html(data);
},
error: function () {
alert('Error occur...!!');
}
});
}
});
});
</script>
</div>
<?php echo form_close(); ?>
控制器
public function item_update($id)
{
$this->load->model('item_model');
$this->form_validation->set_rules('up_code', 'Product Code', 'required');
$this->form_validation->set_rules('up_name', 'Product Description', 'required');
$this->form_validation->set_rules('up_price', 'Price', 'required');
$cur_name = $this->input->post('cur_name');
$cur_code = $this->input->post('cur_code');
$cur_category = $this->input->post('cur_category');
$cur_price = $this->input->post('cur_price');
$cur_brand = $this->input->post('cur_brand');
$cur_sizes = $this->input->post('cur_sizes');
$up_name = $this->input->post('up_name');
$up_category = $this->input->post('up_category');
$up_code = $this->input->post('up_code');
$up_price = $this->input->post('up_price');
$up_sizes = $this->input->post('up_sizes');
$up_brand = $this->input->post('up_brand');
if($cur_code === $up_code && $cur_name === $up_name && $cur_brand === $up_brand && $cur_category === $up_category && $cur_sizes === $up_sizes && $cur_price === $up_price)
{
$this->session->set_flashdata('successMessage', '<div class="alert alert-info">No Changes</div>');
redirect(base_url('home'));
}
else
{
if($this->form_validation->run() == FALSE)
{
$this->session->set_flashdata('errorMessage',
'<div class="alert alert-danger">Opss Something Went Wrong Updating The Item. Please Try Again.</div>');
redirect(base_url('home'));
}
else
{
$this->load->model('item_model');
$update = $this->item_model->update_item($id, $up_code, $up_name, $up_brand, $up_category, $up_size,
$up_price);
if($update)
{
$this->session->set_flashdata('successMessage',
'<div class="alert alert-success">Item Updated</div>');
redirect(base_url('home'));
}
}
}
}
和我的模特
public function update_item($id, $up_code, $up_name, $up_brand, $up_category,
$up_sizes, $up_price)
{
$this->load->database();
$data = array(
'prod_code' => "$up_code",
'prod_desc' => "$up_name",
'brand_id' => "$up_brand",
'category_id' => "$up_category",
'size_id' => "$up_sizes",
'original_price' => "$up_price",
);
$this->db->where('id', $id);
return $this->db->update('products', $data);
}
它只显示消息&#34; Opss Something Went Wound更新项目。请再试一次&#34;