dqkx69935 2016-04-21 13:27 采纳率: 100%
浏览 71
已采纳

在prestashop中为后台产品添加了选项卡和字段 - 如何正确保存输入?

I followed this tutorial on creating a custom module for a new tab and input in the back office

I don't want anything to do with languages - I have a simple text input where the user will input an INT.

In the tutorial it has us update the hookActionProductUpdate method:

public function hookActionProductUpdate($params)
{
// get the lines
// store the new field

$id_product = (int)Tools::getValue('id_product');
    if(!Db::getInstance()->update('number_lines', array('number_lines'=> pSQL(Tools::getValue('number_lines'))).' AND id_product = ' .$id_product ))
        $this->context->controller->_errors[] = Tools::displayError('Error: ').mysql_error();
}    

When I click 'save & stay' nothing happens.

My .tpl is showing it like this:

<div class="col-lg-1">
    <input type="text" name="number_lines" id="number_lines" value="{$number_lines|htmlentities}" />
</div>

Any help would be appreciated.

Overall purpose is just to have a text field that the user can enter a number into and it's saved into the database in the back office of a product.

  • 写回答

2条回答 默认 最新

  • douqiang4501 2016-04-21 16:22
    关注

    I guess you have table called ps_number_lines with columns id_product and number_lines, so your update query should looks like:

    Db::getInstance()->update('number_lines', 
        array('number_lines' => pSQL((int)Tools::getValue('number_lines'))), 
        'id_product = ' . $id_product)
    

    because now you use it wrong, for better understanding check method update in classes/db/Db.php

    /**
         * @param string $table Table name without prefix
         * @param array $data Data to insert as associative array. If $data is a list of arrays, multiple insert will be done
         * @param string $where WHERE condition
         * @param int $limit
         * @param bool $null_values If we want to use NULL values instead of empty quotes
         * @param bool $use_cache
         * @param bool $add_prefix Add or not _DB_PREFIX_ before table name
         * @return bool
         */
        public function update($table, $data, $where = '', $limit = 0, $null_values = false, $use_cache = true, $add_prefix = true)
        {
            if (!$data)
                return true;
    
            if ($add_prefix)
                $table = _DB_PREFIX_.$table;
    
            $sql = 'UPDATE `'.bqSQL($table).'` SET ';
            foreach ($data as $key => $value)
            {
                if (!is_array($value))
                    $value = array('type' => 'text', 'value' => $value);
                if ($value['type'] == 'sql')
                    $sql .= '`'.bqSQL($key)."` = {$value['value']},";
                else
                    $sql .= ($null_values && ($value['value'] === '' || is_null($value['value']))) ? '`'.bqSQL($key)."` = NULL," : '`'.bqSQL($key)."` = '{$value['value']}',";
            }
    
            $sql = rtrim($sql, ',');
            if ($where)
                $sql .= ' WHERE '.$where;
            if ($limit)
                $sql .= ' LIMIT '.(int)$limit;
            return (bool)$this->q($sql, $use_cache);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)