Prestashop 1.6.1如何将tpl文件中表单的输入数据发布到mysqldb


来自希腊的问候,

我正试图将位于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;
}
}

标准方式:

您必须添加";actionProductUpdate";挂接您的模块。

此外,您必须拥有自定义的objectModel。

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

注意:这只是一个没有经过验证的示例代码。。。

最新更新