我希望更新我的自定义模块的 sql 值,但由于此值位于单独的 sql 表中,因此我卡住了......
第一个值 MY_TOPBAR 已正确更新,因为她在ps_configuration表中更新,但我的第二个值 MY_HTML_DATA 在我的表ps_mymodule中未正确更新。
这是我的php代码:
public function getContent()
{
// If we try to update the settings
$output = '';
if (Tools::isSubmit('submit'.$this->name))
{
Configuration::updateValue('MY_TOPBAR', Tools::getValue('MY_TOPBAR', ''));
Configuration::updateValue('MY_HTML_DATA', Tools::getValue('MY_HTML_DATA', ''));
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&tab_module='.$this->tab.'&conf=4&module_name='.$this->name);
}
return $output.$this->displayForm();
}
如何正确更新我的MY_HTML_DATA值?
谢谢
#我终于使用了@yenshirak提供的方法,这里是允许更新我的自定义表的最终代码,而不会丢失任何 html 数据 =>
public function getContent()
{
// If we try to update the settings
$output = '';
if (Tools::isSubmit('submit'.$this->name))
{
Configuration::updateValue('MY_TOPBAR', Tools::getValue('MY_TOPBAR', ''));
$MY_HTML_DATA = pSQL( Tools::getValue('MY_HTML_DATA', ''), true );
$sql='UPDATE `'._DB_PREFIX_.'mymodule` SET `data` = "'.$MY_HTML_DATA.'" WHERE `option` =''MY_HTML_DATA'';';
if(Db::getInstance()->Execute($sql))
Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules').'&configure='.$this->name.'&tab_module='.$this->tab.'&conf=4&module_name='.$this->name);
}
return $output.$this->displayForm();
}
您应该使用 Db
类来更新自定义表:
$sql = 'UPDATE ' . _DB_PREFIX_ . 'mymodule SET your_column_name = "' . pSQL(Tools::getValue('MY_HTML_DATA', '')) . '" WHERE id = your_id';
Db::getInstance()->execute($sql);
文档:数据库类最佳实践
试试看:
$query = "UPDATE "._DB_PREFIX_."attribute SET color= '".$imagename."' WHERE id_attribute_group = '".$option_id."'";
Db::getInstance()->Execute($query);