【问题标题】:SQL INSERT INTO table with different params具有不同参数的 SQL INSERT INTO 表
【发布时间】:2020-05-13 22:20:57
【问题描述】:

我目前正在使用 PHP 和 MySQL 开发餐厅 POS 系统,并且有一个包含以下值的表 (open_tables): code、idSeller、tableNo、products、netPrice

现在我想打开表格,进行更改并将其保存到带有额外参数的不同表格(销售)中: code, idSeller, tableNo, idCustomer, products, netPrice, discount, totalPrice, paymentMethod。

我该怎么办?

这是我目前拥有的,但它给了我错误:

public static function ReopenSaleModel($table1, $table2, $data){

        $stmt = Connection::connect()->prepare("INSERT INTO $table2 SELECT * FROM $table1 WHERE code, idSeller, tableNo, idCustomer, products, netPrice, discount, totalPrice, paymentMethod VALUES :code, :idSeller, :tableNo, :idCustomer, :products, :netPrice, :discount, :totalPrice, :paymentMethod");

        $stmt->bindParam(":code", $data["code"], PDO::PARAM_INT);
        $stmt->bindParam(":idSeller", $data["idSeller"], PDO::PARAM_INT);
        $stmt->bindParam(":tableNo", $data["tableNo"], PDO::PARAM_STR);
        $stmt->bindParam(":idCustomer", $data["idCustomer"], PDO::PARAM_STR);
        $stmt->bindParam(":products", $data["products"], PDO::PARAM_STR);
        $stmt->bindParam(":netPrice", $data["netPrice"], PDO::PARAM_STR);
        $stmt->bindParam(":discount", $data["discount"], PDO::PARAM_STR);
        $stmt->bindParam(":totalPrice", $data["totalPrice"], PDO::PARAM_STR);
        $stmt->bindParam(":paymentMethod", $data["paymentMethod"], PDO::PARAM_STR);

        if($stmt->execute()){

            return "ok";

        }else{

            return "error";

        }

        $stmt->close();
        $stmt = null;

    }

【问题讨论】:

    标签: php mysql sql select sql-insert


    【解决方案1】:

    看起来您正在寻找insert ... select 语法。这看起来像:

    INSERT INTO sales (
        code, 
        idSeller, 
        tableNo, 
        products, 
        netPrice,
        idSeller, 
        tableNo, 
        idCustomer, 
        products, 
        netPrice, 
        discount, 
        totalPrice, 
        paymentMethod
    )
    SELECT 
        code, 
        idSeller, 
        tableNo, 
        products, 
        netPrice,
        :idSeller, 
        :tableNo, 
        :idCustomer, 
        :products, 
        :netPrice, 
        :discount, 
        :totalPrice, 
        :paymentMethod
    FROM open_tables 
    

    请注意,我列出了insert 的所有目标列:这是一种使错误更容易解决的好习惯(您可能需要调整目标表的实际列名)

    【讨论】:

    • 它现在给我一个“无效的参数号:绑定变量的数量与令牌的数量不匹配”错误。
    • 那是因为你正在绑定参数:code,而它没有出现在查询中。也许你想要INSERT ... SELECT :code, ... FROM ... 而不是INSERT ... SELECT code, ... FROM ...?或者它可能转到WHERE 子句?如果没有看到更多信息,我无法判断,您需要调查一下。
    【解决方案2】:

    感谢您的回复。使用以下方法让它工作:

    public static function ReopenSaleModel($table1, $table2, $data){
    
            $stmt = Connection::connect()->prepare("INSERT INTO $table2(code, idSeller, tableNo, idCustomer, products, netPrice, discount, totalPrice, paymentMethod) SELECT (:code), (:idSeller), (:tableNo), (:idCustomer), (:products), (:netPrice), (:discount), (:totalPrice), (:paymentMethod) FROM $table1");
    
            $stmt->bindParam(":code", $data["code"], PDO::PARAM_INT);
            $stmt->bindParam(":idSeller", $data["idSeller"], PDO::PARAM_INT);
            $stmt->bindParam(":tableNo", $data["tableNo"], PDO::PARAM_STR);
            $stmt->bindParam(":idCustomer", $data["idCustomer"], PDO::PARAM_STR);
            $stmt->bindParam(":products", $data["products"], PDO::PARAM_STR);
            $stmt->bindParam(":netPrice", $data["netPrice"], PDO::PARAM_STR);
            $stmt->bindParam(":discount", $data["discount"], PDO::PARAM_STR);
            $stmt->bindParam(":totalPrice", $data["totalPrice"], PDO::PARAM_STR);
            $stmt->bindParam(":paymentMethod", $data["paymentMethod"], PDO::PARAM_STR);
    
            if($stmt->execute()){
    
                return "ok";
    
            }else{
    
                return "error";
    
            }
    
            $stmt->close();
            $stmt = null;
    
        }
    

    【讨论】:

      猜你喜欢
      • 2020-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-09
      • 2014-04-23
      • 1970-01-01
      • 2023-02-21
      相关资源
      最近更新 更多