mysql 从prestashop模块连接到数据库

5sxhfpxr  于 2022-12-10  发布在  Mysql
关注(0)|答案(1)|浏览(123)

模块无法与数据库连接。
我有一个prestashop模块,当这个钩子启动时,它必须与数据库一起执行一项任务:hookActionPaymentConfirment。但我什么也做不了,我不知道是连接有问题还是查询有问题。这是hookActionPayment确认函数的代码,您看有什么错误吗?任务是更新同一数据库的表中的产品库存。我想使用数据库的名称和服务器的名称,因为我计划在外部数据库中实现该表。

`    public function hookActionPaymentConfirmation($params)
    {
        //mail("luilli.guillan@gmail.com", "yiha", "hola");
        $database=Configuration::get('MIMODULOMISMADB_ACCOUNT_NOMBREDB', null);
        $user=Configuration::get('MIMODULOMISMADB_ACCOUNT_USUARIO', null);
        $password=Configuration::get('MIMODULOMISMADB_ACCOUNT_PASSWORD', null);
        //
        $db = new DbMySQLi("localhost",$user,$password,$database,true);
        
        //$products = $params['cart']->getProducts(true);//en los nuevos ps ya no va y hay que hacerlo con las dos ordenes siguientes
        $order = new Order($params['id_order']);
    $products = $order->getCartProducts();
    
        foreach ($products as $product) 
        {
            $id_product = $product['id_product'];
            $cantidad = $product['cart_quantity'];
            $referencia = $product['reference'];
            $product_attribute_id = $product['product_attribute_id'];
            
            $newProduct = new Product($id_product);
            if($newProduct->hasCombinations())
            {
                $sql = 'select * from product_attribute where id_product_attribute = ' . (string) $product_attribute_id . ';';
                //$rec = DB::getInstance()->getRow($sql);
                $rec = $db->getValue($sql);
                $referencia = $rec["reference"];
                //mail("luilli.guillan@gmail.com", "has combinations", $id_product." ".$referencia." ".$cantidad." p.a: ".$product_attribute);
            }
            
            $unidades = $db->getValue('SELECT unidades FROM productos WHERE '.$campoid.' = "'.$referencia.'"');         
            $unidadesRestantes=$unidades-$cantidad;
            $db->Execute('UPDATE productos SET unidades="'.$unidadesRestantes.'" WHERE '.$campoid.' = "'.$referencia.'"');          
            mail("luilli.guillan@gmail.com", "yay",$database." ".$user." ".$password." ".$unidades);
            //mail("luilli.guillan@gmail.com", "yay",$unidades);                        
    }   
    }`
xdnvmnnf

xdnvmnnf1#

我已经解决了。缺少以下行:$campoid = Configuration::get('CAMPOID', null);这是钩子的完整代码:

public function hookActionPaymentConfirmation($params) {
    
    
    
    $database = Configuration::get('MIMODULOMISMADB_ACCOUNT_NOMBREDB', null);
    $user = Configuration::get('MIMODULOMISMADB_ACCOUNT_USUARIO', null);
    $password = Configuration::get('MIMODULOMISMADB_ACCOUNT_PASSWORD', null);
    $campoid = Configuration::get('CAMPOID', null);

    $order = new Order($params['id_order']);
    $products = $order->getCartProducts();
    
try {
        $db = new DbMySQLi("localhost", $user, $password, $database, true);
    } catch (PrestaShopDatabaseException $ex) {
    
        $dbLocal = \Db::getInstance();
        foreach ($products as $product) {
            $cantidad = $product['cart_quantity'];
            $referencia = $product['reference'];
            $dbLocal->Execute("insert into bufferQuerys (referencia,unidadesCompradas) values('" . $referencia . "','" . $cantidad . "')");
           
            
        }
        
    }
    
    
    if ($db) 
    {
        foreach ($products as $product) {
            $id_product = $product['id_product'];
            $cantidad = $product['cart_quantity'];
            $referencia = $product['reference'];
            $product_attribute_id = $product['product_attribute_id'];

            $newProduct = new Product($id_product);
            if ($newProduct->hasCombinations()) {
                $sql = 'select * from product_attribute where id_product_attribute = ' . (string) $product_attribute_id . ';';
                $rec = DB::getInstance()->getRow($sql);
                //$rec = $db->getValue($sql);
                $referencia = $rec["reference"];
                
            }

            $unidades = $db->getValue('SELECT unidades FROM productos WHERE ' . $campoid . ' = "' . $referencia . '"');
            $unidadesRestantes = $unidades - $cantidad;
            $db->Execute('UPDATE productos SET unidades="' . $unidadesRestantes . '" WHERE ' . $campoid . ' = "' . $referencia . '"');
            
        }
    } 
   
}

变量campoid在以下语句中使用:$db->Execute('UPDATE productos SET unidades="' . $unidadesRestantes . '" WHERE ' . $campoid . ' = "' . $referencia . '"');,但没有定义它。

相关问题