我有从OpenCart(mysql)数据库中提取的值,我将它们提供给一个表单,然后用户可以提交并使用它来更新他们的产品表。然而,当我运行下面的代码时,1)没有出现错误,2)当我检查mysql数据库时,我看到了列,但它仍然是0,而不是我想要的值(例如1、4、5等)
型号:
public function unit_id_update() {
$product_id = $this->db->getLastId();
$this->db->query("UPDATE " . DB_PREFIX . "product SET unit_class_id = " . $this->db->$unit_type_id . " WHERE product_id = " . (int)$product_id);
}
public function unit_id_request() {
$w_data = $this->db->query("SELECT weight_class_id FROM ". DB_PREFIX ." weight_class_description");
if (!$w_data) {
die(mysql_error());
}
else {
return $w_data;
}
}
控制器:
$this->load->model('catalog/product'); //where functions are located
$another_var = $this->model_catalog_product->unit_id_request();
$this->data['weight_var'] = $another_var;
$this->data['kg'] = $another_var->rows[0]['weight_class_id'];
$this->data['g'] = $another_var->rows[1]['weight_class_id'];
$this->data['lb'] = $another_var->rows[2]['weight_class_id'];
$this->data['oz'] = $another_var->rows[3]['weight_class_id'];
if (isset($this->request->post['unit_type_id'])) {
$this->data['unit_type_id'] = $this->request->post['unit_type_id'];
}
else {
$this->data['unit_type_id'] = '';
}
视图:
<tr>
<td><?php echo "Unit Type"; ?></td></tr>
<td><select name="unit_type_id">
<optgroup label="<?php echo "Weight"; ?>">
<option value="<?php echo $lb; ?>" >lb</option>
<option value="<?php echo $oz; ?>">oz</option>
<option value="<?php echo $kg; ?>">kg</option>
<option value="<?php echo $g; ?>">g</option>
</optgroup>
</select></td>
</tr>
-
相信我,你不希望这个
die(mysql_error());
在生产中。以不同的方式处理这种情况(例如,返回一个空数组并记录错误)。 -
问题在于整个
unit_id_update()
方法。您应该将$product_id
和$unit_type_id
变量作为参数从控制器中传递(即处理POST请求)。
方法应该看起来像:
public function unit_id_update($product_id, $unit_type_id) {
$this->db->query("UPDATE " . DB_PREFIX . "product SET unit_class_id = " . (int)$unit_type_id . " WHERE product_id = " . (int)$product_id);
}
尝试在unit_id_update()中的查询中在"product"之前插入一个空格并进行检查。
$this->db->query("UPDATE " . DB_PREFIX . " product SET unit_class_id = " . $this->db->$unit_type_id . " WHERE product_id = " . (int)$product_id);