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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化