【问题标题】:Insert data from session array to prestashop database using php使用php将数据从会话数组插入prestashop数据库
【发布时间】:2014-02-17 16:43:09
【问题描述】:

我正在开发一个自定义 prestashop 模块,该模块从另一个数据库(外部数据库)检索数据,然后将数据插入 prestashop 数据库。我制作了一个 php 文件,它从外部数据库中检索数据并保存到会话变量中。

我从那个 php 文件中得到了这个数组: [0] => Array ( [0] => 1 [productid] => 1 [1] => 0 [parent] => 0 [2] => 0 [3] => iPod Shuffle [prodname] => iPod Shuffle 这是它的代码:`FILE)。 "/settings.inc.php");

//database 1
//$data = array();

$conn = @mysql_connect($GLOBALS['VCS_CFG']["dbServer"],$GLOBALS['VCS_CFG']["dbUser"], $GLOBALS['VCS_CFG']["dbPass"]);
if ($conn){
    if (mysql_select_db($GLOBALS['VCS_CFG']["dbDatabase"])) {
        $SQL = "SELECT * FROM vc_products";
        $q = mysql_query($SQL);

        while($row = mysql_fetch_array($q))
        {
            $json_output[] = $row;
            $_SESSION['myData'] = $json_output;
        }
        //echo json_encode($json_output);
        print_r($_SESSION['myData']);
    }
    mysql_close($conn);
}

`

我试图在表 ps_order_detail 表的 product_name 行中插入行 prodname。现在我正在制作 prestashop 模块,它将将该数据插入 Prestashop 数据库。这是我的代码:

这是我模块的数据插入代码:

<?php
if (!defined('_PS_VERSION_'))
    exit;

include 'test.php';

class PrestaBridge extends Module
{

    public function __construct()
    {
        $this->name = 'PrestaBridge';
        $this->tab = 'Front';
        $this->version = 1.5;
        $this->author = 'Sergio Kagiema';
        $this->need_instance = 0;

        parent::__construct();

        $this->displayName = $this->l('PrestaBridge');
        $this->description = $this->l('A module for transferring data from Vcart to Prestashop!');
        $this->confirmUninstall = $this->l('Are you sure you want to uninstall?');

        if (!Configuration::get('PrestaBridge'))      
            $this->warning = $this->l('No name provided');

    } 


    //INSTALL TOY MODULE
    public function install()
    {
        $parent_tab = new Tab();
        foreach (Language::getLanguages(true) as $lang)
            $parent_tab->name [$lang['id_lang']] = 'PrestaBridge';
        $parent_tab->class_name = 'BridgePage';
        $parent_tab->id_parent = 0;
        $parent_tab->module = $this->name;
        $parent_tab->add();
        if (!parent::install()
            || !$this->installModuleTab('BridgePage', array((int)(Configuration::get('PS_LANG_DEFAULT'))=>'PrestaBridge'), $parent_tab->id)
        )
            return false;
        return true;
    }



    //UNISTALL TOY MODULE
    public function uninstall()
    {
        if (!parent::uninstall()
            || !$this->uninstallModuleTab('BridgePage')
        )
            return false;
        return true;
    }   

    private function installModuleTab($tabClass, $tabName, $idTabParent)
    {
        $idTab = Tab::getIdFromClassName($idTabParent);       
        $idTab = $idTabParent;
        $pass = true ;
        @copy(_PS_MODULE_DIR_.$this->name.'/logo.gif', _PS_IMG_DIR_.'t/'.$tabClass.'.gif');
        $tab = new Tab();
        $tab->name = $tabName;
        $tab->class_name = $tabClass;
        $tab->module = $this->name;
        $tab->id_parent = $idTab;
        $pass = $tab->save();
        return($pass);
    }


    private function uninstallModuleTab($tabClass)
    {
        $pass = true ;
        @unlink(_PS_IMG_DIR_.'t/'.$tabClass.'.gif');
        $idTab = Tab::getIdFromClassName($tabClass);
        if($idTab != 0)
        {
            $tab = new Tab($idTab);
            $pass = $tab->delete();
        }
        return($pass);
    }                                                                                




    public function getContent() {
        $this->_html = '<h2>'.$this->displayName.'</h2>';

        if (Tools::isSubmit('submit')) {
            $sql = 'INSERT INTO '._DB_PREFIX_.'order_detail(product_id, product_name) VALUES';
            $valuesArr = array();
            if  ($data = Db::getInstance()->Execute($sql))
                foreach ($data as $row){
                    $product_id = (int) $row['productid'];
                    $product_name = $row['prodname']; 

                    $valuesArr[] = "('$product_id', '$product_name')";
            }
             $sql .= implode(',', $valuesArr);

            $this->_displayForm();
            return $this->_html;
        }
    } 


    private function _displayForm() {

        $this->_html .= '<div class="clear"></div>';

        $this->_html .= '<div class="bridge" id="bridge">';
        $this->_html .= '<form method="post" action="'.$_SERVER['REQUEST_URI'].'" id="test">';
        $this->_html .= '<input type="text" name="username" id="username"/>';
        $this->_html .= '<input type="text" name="password" id="password"/>';
        $this->_html .= '<button id="myButton" onclick="myfunc()" type="submit">Transfer Data</button>';
        $this->_html .= '</div>';
    }



}

?>

你能帮帮我吗?

【问题讨论】:

  • 我们在谈论什么版本?你想在哪里连接?模块 ?在 prestashop 中,有一种从数据库中添加/选择/更新/删除的严格方法。请回答我的问题,我会帮助你
  • 这是关于 Prestashop 1.5 的。我正在制作一个将数据插入 Prestashop 数据库的模块。所以,我正在尝试使用 ftp 连接到我的数据库。

标签: php sql prestashop


【解决方案1】:

你应该从这个http://doc.prestashop.com/display/PS15/DB+class+best+practices开始

告诉您需要了解的有关最佳做法的所有信息。无论如何,假设您正在创建一个模块。这意味着您正在扩展一个 Module 类,并且您的模块是这样开始的

class yourCustomeModuleName extends Module
    {
    }

在这些标签之间插入所有相关数据,如公共 __construct 函数和所有其他所需的函数,包括要显示数据的挂钩函数。

要通过 Prestashop 将某些内容插入数据库,您需要使用 execute() 命令什么是 Prestashop 中的全局变量。这是我为您提供的链接中的一个示例

$sql = 'DELETE FROM '._DB_PREFIX_.'product WHERE active = 0';
if (!Db::getInstance()->execute($sql))
die('Error etc.)';

这意味着 - 当您已经在班级内时,您不需要执行数据库连接。如果这对您来说是新的,那么我建议您阅读 OOP(面向对象编程)

【讨论】:

    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2023-03-05
    • 1970-01-01
    • 2012-04-20
    相关资源
    最近更新 更多