简体   繁体   English

使用php将数据从会话数组插入到prestashop数据库

[英]Insert data from session array to prestashop database using php

Im working on a custom prestashop module that retrieves data from another database(an external database) and then it inserts the data to the prestashop data base.我正在研究一个自定义 prestashop 模块,该模块从另一个数据库(外部数据库)中检索数据,然后将数据插入到 prestashop 数据库中。 I've made a php file that retrieves data from the external database and saves to a session variable.我制作了一个 php 文件,它从外部数据库中检索数据并保存到会话变量中。

I've got this array from that php file: [0] => Array ( [0] => 1 [productid] => 1 [1] => 0 [parent] => 0 [2] => 0 [3] => iPod Shuffle [prodname] => iPod Shuffle And this is the its code: `FILE) .我从那个 php 文件中得到了这个数组: [0] => Array ( [0] => 1 [productid] => 1 [1] => 0 [parent] => 0 [2] => 0 [3] => iPod Shuffle [prodname] => iPod Shuffle这是它的代码:`FILE)。 "/settings.inc.php"); "/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);
}

` `

im trying to insert the row prodname at the product_name row of the table ps_order_detail table.我试图在表 ps_order_detail 表的 product_name 行中插入行 prodname。 Now im making the prestashop module that it will insert that data to Prestashop database.现在我正在制作 prestashop 模块,它将将该数据插入到 Prestashop 数据库中。 Here is my code:这是我的代码:

This is my module's code for the data insert:这是我的数据插入模块的代码:

<?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>';
    }



}

?> ?>

Can you help me out please?你能帮我吗?

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

Tells you everything you need to know about best practices.告诉您有关最佳实践的所有信息。 Anyways let's say you are creating a module.无论如何,假设您正在创建一个模块。 It means you are extending a Module class and your module starts like this这意味着您正在扩展一个 Module 类,并且您的模块像这样开始

class yourCustomeModuleName extends Module
    {
    }

Between those tags you insert all the relevant data like public __construct function and all the other functions you need, including the hook functions that you want to display your data.在这些标签之间插入所有相关数据,如 public __construct 函数和您需要的所有其他函数,包括您想要显示数据的钩子函数。

To insert something in to database through Prestashop you need to use execute() command what is a global variable in Prestashop.要通过 Prestashop 将内容插入数据库,您需要使用 execute() 命令,这是 Prestashop 中的全局变量。 Here is a example from the link that I provided you with这是我为您提供的链接中的一个示例

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

So that means - you don't need to execute database connections when you are already inside of a class.所以这意味着 - 当你已经在一个类中时,你不需要执行数据库连接。 If this is new to you then I suggest you to read about OOP (object oriented programming)如果这对你来说是新的,那么我建议你阅读 OOP(面向对象编程)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM