drk7700 2015-12-02 15:28
浏览 44
已采纳

MySQL查询不插入在PHP中添加的数组元素

I have got a problem with my php algorithm. It is supposed to insert the items ordered by a customer into a database. When the order total is less than 12 pounds and is to be delivered, a delivery charge is to be added to the multi-dimensional array $orderItems. When the order total is greater than or equal to 20 pounds, a free item is to be added to this array. The response to the browser shows that the array is being added to correctly but the SQL query doesn't seem to contain the items I'm adding to the array within the PHP. Any other items already in the array are added without problems and show in the database fine.

I would like help with getting my algorithm to add all the items in the $orderItems multi-dimensional array.

The PHP:

if (($orderTotal < 12.00) && ($deliveryorcollection == "Delivery") == TRUE)
{
    array_push($orderItems, array('dish_id' => "1F", 'dish_name' => "Delivery Charge 1", 'dish_size' => "Regular", 'dish_quantity' => 1, 'dish_price' => 1.00));
}
if ($orderTotal >= 20.00)
{
    array_push($orderItems, array('dish_id' => "1E", 'dish_name' => "Mini Vegetarian Spring Rolls", 'dish_size' => "Regular", 'dish_quantity' => 1, 'dish_price' => 0.00));
}

var_dump($orderItems);

foreach ($orderItems as $k => $cur)
{
    $sql = "INSERT INTO `airsofto_YummyYummy`.`ItemOrders` (`OrderID`, `DishID`, `OrderQuantity`, `DishSize`) VALUES ('$orderid', '$cur->dish_id', '$cur->dish_quantity', '$cur->dish_size')";
    //Line where the error occurs^
    if ($conn->query($sql) === TRUE)
    {
        echo "New itemOrder record created successfully";
    }
    else
    {
        echo "Error: " . $sql . "<br>" . $conn -> error;
    }
}

This is what it printed to the browser:

array(2) { [0]=> object(stdClass)#1 (5) { ["dish_id"]=> int(55) ["dish_name"]=> string(17) "Chicken Chow Mein" ["dish_size"]=> string(5) "Large" ["dish_quantity"]=> string(1) "1" ["dish_price"]=> float(4.6) } [1]=> array(5) { ["dish_id"]=> string(2) "1F" ["dish_name"]=> string(17) "Delivery Charge 1" ["dish_size"]=> string(7) "Regular" ["dish_quantity"]=> int(1) ["dish_price"]=> float(1) } } New itemOrder record created successfullyError: INSERT INTO airsofto_YummyYummy.ItemOrders (OrderID, DishID, OrderQuantity, DishSize) VALUES ('161', '', '', '') Cannot add or update a child row: a foreign key constraint fails (airsofto_YummyYummy.ItemOrders, CONSTRAINT ItemOrders_ibfk_2 FOREIGN KEY (DishID) REFERENCES Dishes (DishID))

You can see that the array is being added to successfully but the SQL query doesn't contain the dish_id, dish_name, dish_size, dish_quantity or dish_price from the array element containing the added delivery charge.

I will note that the DishID column in the database is a varchar.

Many thanks in advance. If there is any other information that could be helpful just leave a comment.

  • 写回答

1条回答 默认 最新

  • dongpao1873 2015-12-02 15:58
    关注

    JonStirling commented with the answer. Thanks!

    I needed to cast my array as an object...

    array_push($orderItems, (object) array('dish_id' => "1F", 'dish_name' => "Delivery Charge 1", 'dish_size' => "Regular", 'dish_quantity' => 1, 'dish_price' => 1.00));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘