首页 > 解决方案 > php多次调用循环中的类函数问题

问题描述

我对其中一个 API 有疑问,该 API 将在其中创建订单信息。每个订单代表 1 个拥有多个商品的买家。我想在我的 API 方法中循环这些项目。但是,当我尝试循环该函数时,它只存储第一个数组值,这不是我想要的。我想将整个值数组存储到数据库中。

这是API代码:

$s = $data->ItemIDs;
if($order->CreateOrderInfo()){
    echo 'Order created successfully.';
    echo "---";
    
    for($i = 0; $i < sizeof($s); $i++){
        $order->ItemCode = $s[$i];
        $order->GetItemInfo();
        if ($order->ItemCode != null){
            $item_array = array(
                'UserMememberNo' => $order->UserMememberNo,
                'Image' => $order->Image,
                'Description' => $order->Description,
                'Price' => $order->Price,
                'ItemBrand' => $order->ItemBrand,
                'ItemModel' => $order->ItemModel
            );

            //GET DATA FOR ITEM TABLE
            $order->ItemGUID = $data->ItemGUID;
            $order->ItemName = $order->Description;
            $order->ItemPrice = $order->Price;
            $order->ItemQuantity = $data->ItemQuantity;
            $order->ItemVariation1 = $data->ItemVariation1;
            $order->ItemVariation2 = $data->ItemVariation2;
            $order->ItemVariation3 = $data->ItemVariation3;
            $order->ItemVariation4 = $data->ItemVariation4;
            $order->ItemVariation5 = $data->ItemVariation5;
            
           }
           else{
                echo json_encode('No item Found');
           }
           
            if($order->CreateItemInfo())
            {
                echo 'Item created';
            }else{
                echo 'Unable to create item';
                echo '----';
            }
    }
    
    if($order->CreateBuyerInfo()){
        echo 'Buyer information created';
    
    }else{
        echo 'Unable to create buyer information';
        
    }

这是模型。此模型用于将值插入数据库:

//CREATE FUNCTION FOR ITEM TABLE
    public function CreateItemInfo() {
        
            $sqlQuery = "INSERT INTO ".$this->item_info_table."
        SET
            GUID = :GUID,
            ItemGUID = :ItemGUID,
            ItemName = :ItemName,
            ItemPrice = :ItemPrice,
            ItemQuantity = :ItemQuantity,
            ItemVariation1 = :ItemVariation1,
            ItemVariation2 = :ItemVariation2,
            ItemVariation3 = :ItemVariation3,
            ItemVariation4 = :ItemVariation4";
        
        $stmt = $this->conn->prepare($sqlQuery);   
        
        $this->GUID = htmlspecialchars(strip_tags($this->GUID));
        $this->ItemGUID = htmlspecialchars(strip_tags($this->ItemGUID));
        $this->ItemName = htmlspecialchars(strip_tags($this->ItemName));
        $this->ItemPrice = htmlspecialchars(strip_tags($this->ItemPrice));
        
        $this->ItemQuantity = htmlspecialchars(strip_tags($this->ItemQuantity));
        $this->ItemVariation1 = htmlspecialchars(strip_tags($this->ItemVariation1));
        $this->ItemVariation2 = htmlspecialchars(strip_tags($this->ItemVariation2));
        $this->ItemVariation3 = htmlspecialchars(strip_tags($this->ItemVariation3));
        $this->ItemVariation4 = htmlspecialchars(strip_tags($this->ItemVariation4));
        
        
        $stmt->bindParam(":GUID", $this->GUID);
        $stmt->bindParam(":ItemGUID", $this->ItemGUID);
        $stmt->bindParam(":ItemName", $this->ItemName);
        $stmt->bindParam(":ItemPrice", $this->ItemPrice);
        $stmt->bindParam(":ItemQuantity", $this->ItemQuantity);
        $stmt->bindParam(":ItemVariation1", $this->ItemVariation1);
        $stmt->bindParam(":ItemVariation2", $this->ItemVariation2);
        $stmt->bindParam(":ItemVariation3", $this->ItemVariation3);
        $stmt->bindParam(":ItemVariation4", $this->ItemVariation4);
        
        if($stmt->execute()) {
            return true;
        }
         return false;
    }

下面的屏幕截图显示了该问题,其中只有数组的第一个值成功插入到数据库中。

数据库插入的结果

标签: phpapipdo

解决方案


推荐阅读