【问题标题】:Prestashop 1.6.1 How to post input data from form in tpl file to mysql db?Prestashop 1.6.1 如何将输入数据从 tpl 文件中的表单发布到 mysql db?
【发布时间】:2021-05-10 21:34:41
【问题描述】:
来自希腊的问候,

我正在尝试将一些输入数据从位于 tpl 文件中的表单发布到我的数据库中的表中。

我尝试了很多方法都没有成功。 (我是 prestashop 模块开发的新手)

提前感谢您的帮助。

我创建表单的文件是: 项目模块.tpl

<h4>{l s='Project Module' mod='projectmodule'}</h4>
<div class="separation"></div>
<form name="text_fields" method="post" action="">
    <table>
        
        <tr>
            <td class="col-left">
                <label>{l s='First Field:'}</label>
            </td>
            <td>
                {include file="controllers/products/input_text_lang.tpl"
                    languages=$languages
                    input_name='firstfield'
                    input_value=$firstfield}
                <p class="preference_description"></p>
            </td>
        </tr>

        <tr>
            <td class="col-left">
                <label>{l s='Second Field:'}</label>
            </td>
            <td>
                {include file="controllers/products/input_text_lang.tpl"
                    languages=$languages
                    input_name='secondfield'
                    input_value=$secondfield}
                <p class="preference_description"></p>
            </td>
        </tr>

        <tr>
            <td class="col-left">
                <label>{l s='Third Field:'}</label>
            </td>
            <td>
                {include file="controllers/products/input_text_lang.tpl"
                    languages=$languages
                    input_name='thirdfield'
                    input_value=$thirdfield}
                <p class="preference_description"></p>
            </td>
        </tr>

        <tr>
            <td><input type ="submit" name = "submit_form" value = "Submit" /></td>
        </tr>
    </table>
</form>

我的主要 php 文件是: 项目模块.php

public function install()
    {

        include(dirname(__FILE__).'/sql/install.php');

        return parent::install() &&
            $this->registerHook('displayAdminProductsExtra');

        
    }

    public function uninstall()
    {

        include(dirname(__FILE__).'/sql/uninstall.php');

        return parent::uninstall();
    }

    public function prepareNewTab()
    {
        
        $this->context->smarty->assign(array(
            'languages' => $this->context->controller->_languages,
        ));

    }
    
    public function hookDisplayAdminProductsExtra($params)
    {
        if (Validate::isLoadedObject($product = new Product((int)Tools::getValue('id_product'))))
        {
            $this->prepareNewTab();
            return $this->display(__FILE__, 'projectmodule.tpl');
        }
    } 

我在数据库中创建表的文件是:install.php

$sql = array();

$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'projectmodule` (
    `id_projectmodule` int(11) NOT NULL AUTO_INCREMENT,
    PRIMARY KEY  (`id_projectmodule`),
    `firstfield` varchar(255) NOT NULL,
    `secondfield` varchar(255) NOT NULL,
    `thirdfield` varchar(255) NOT NULL    
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8;';

foreach ($sql as $query) {
    if (Db::getInstance()->execute($query) == false) {
        return false;
    }
}

【问题讨论】:

  • “没有任何成功”不是很充分。你做了什么?您是否检查过浏览器控制台是否有错误?您检查过网络服务器错误日志吗?您是否查看过浏览器向服务器发送的内容?服务器是否收到您期望的内容?需要更多细节。您需要缩小问题所在。
  • 我尝试使用 {php} 在 tls 文件中插入以下代码 if (Tools::isSubmit('submit_form')){ Db ::getInstance()->Execute("INSERT INTO ps_projectmodule ( 'firstfield', 'secondfield', 'thirdfield') 值 (tools::getValue('firstfield', 'secondfield', 'thirdfield'))"); } {/php} 并且它没有用,我也尝试过我的表单中的操作,它指向一个具有相同代码的 php 文件,例如FILE) 。 '/../../config/config.inc.php';需要一次目录名(文件)。 '/../../init.php'; if (Tools::isSubmit('submit_form')){
  • Db ::getInstance()->Execute("INSERT INTO ps_projectmodule ('firstfield', 'secondfield', 'thirdfield') VALUE (tools::getValue('firstfield', 'secondfield', '第三场'))");并且它也没有工作。当然,浏览器控制台目前有错误,但即使没有任何错误,它也无法正常工作。即使我看到一些错误日志,我也无法理解我所看到的。
  • 服务器正确创建了表格,但我不知道如何遵循输入数据的路径

标签: php mysql forms post prestashop


【解决方案1】:

标准方式:

你必须在你的模块中添加“actionProductUpdate”钩子。

您还必须有您的自定义对象模型。

public function hookActionProductUpdate($params)
{
    require_once (_PS_MODULE_DIR_ . $this->name .'/models/ProjectModuleCore.php');
    $id_product = (int)Tools::getValue('id_product');
    if($id_product && Tools::getValue('submit_form')) {
        $id = null;
        // Some code to get if exist $id
        $fn = new ProjectModuleCore($id);
        $fn->id_product = $id_product;
        $fn->firstfield = Tools::getValue('firstfield');
        $fn->secondfield = Tools::getValue('secondfield');
        $fn->thirdfield = Tools::getValue('thirdfield');
        $fn->save();
    }
}

在“ProjectModuleCore.php”文件中:

<?php
class iProductFnbCore extends ObjectModel
{
    public $id_product;
    public $firstfield;
    public $secondfield;
    public $thirdfield;

    public static $definition = array(
        'table' => 'projectmodule',
        'primary' => 'id_projectmodule',
        'fields' => array(
            'id_product' =>      array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
            'firstfield' =>              array('type' => self::TYPE_STRING, 'validate' => 'isString'),
            'secondfield' =>         array('type' => self::TYPE_STRING, 'validate' => 'isString'),
            'thirdfield' =>          array('type' => self::TYPE_STRING, 'validate' => 'isString'),
        )
    );
}

注意:这只是一个示例代码,没有验证和...

【讨论】:

  • 首先,我要感谢您的时间和精力。但是还是什么都没有,从表单数据中数据库中没有结果。我在您的代码中更正了 hookActionProductUpdate 函数中的符号“|”而不是“}”,但仍然没有。你能给我一些其他的建议吗?谢谢
  • 我有许多与您的模块相似的模块(带有 nop)。确保模块位于操作产品更新挂钩中。
  • 请原谅我的持久性,但你提到我应该确保模块在 hookActionProductUpdate 中。你是什​​么意思?谢谢!!
  • 只需将此新钩子添加到您的“安装”方法并从管理员重置您的模块
  • 我已经在安装函数中声明了钩子,我也尝试卸载模块,我删除了数据库中的表并重新安装了模块以创建模块和表中的表数据库从一开始。这些都没有奏效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2010-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 2015-11-25
相关资源
最近更新 更多