OpenCart - 尝试从前端更新产品表

时间:2014-09-05 15:08:13

标签: php mysql opencart

我有从我的OpenCart(mysql)数据库中提取的值,我将它们提供给用户可以提交并用于更新其产品表的表单。但是,当我运行下面的代码时,1)没有错误出现,2)当我检查mysql数据库时,我看到列,但它仍然是0而不是我想要的值(例如1,4,5等)。 / p>

型号:

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>

2 个答案:

答案 0 :(得分:0)

  1. 相信我,你不希望这个die(mysql_error());投入生产。以不同的方式处理案例(例如,返回一个空数组并记录错误)。

  2. 问题是整个unit_id_update()方法。您应该将$product_id$unit_type_id变量作为控制器的参数传递(即处理POST请求)。

  3. 该方法应如下所示:

    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);
    }
    

答案 1 :(得分:-2)

尝试在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);