2015-12-02 15:28
浏览 44


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));


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";
        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));
    解决 无用
    打赏 举报