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 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解