循环通过foreach时插入额外的行

时间:2014-08-05 09:13:26

标签: php mysql foreach

我将添加到购物车中的汽车插入三个表中以下订单。首先插入tbl_order。
其次, 如果购物车中有2辆汽车和3个插件,则第二张表插入两排汽车。我使用foreach循环来实现这一目标。第三,对于插件项目,我在另一张桌子上为每辆车插入三次。我也使用了foreach循环 现在,问题是每个订单不是两辆车,而是在桌面上插入三辆汽车。显然额外的行没有车ID。
Sam为插件,而不是每个订单六个插件,它在插件表中插入额外的一行,插件ID为NULL。

以下是代码:

 <?php
//Insert order
$insert="INSERT INTO tbl_order(id,pickup_id,dropoff_id,customer_name,contact_number,remark_by_customer,pay_type,pickup_date,return_date,renting_days,amount,Create_Dt) VALUES ('$u_id','$loca_id','$drop_id','$c_name','$contact',' $c_remark','$payment_method','$date_value','$date_value_2','$r_days','$grand_sum',now())";

   mysql_query($insert)or die(mysql_error());
         $o_id=mysql_insert_id();
         foreach($_SESSION['cart'] as $key=>$val)
         {
          $cars=$val['car_id'];

         $o_id=mysql_insert_id();   //retrieve order id
         //insert into tbl_orderdetail->car items      
         $order_details="INSERT INTO tbl_orderdetail (OrderID,carName_id,Car_Unitprice,comment) VALUES ('$o_id','$cars','$grand_sum','$c_remark')";
   mysql_query($order_details)or die(mysql_error());
         }


         $o_id=mysql_insert_id();//retrieve order id
         foreach($_SESSION['cart'] as $key_1=>$val_1)
         {

            $adds=$val_1['addon'];
            $value_2=explode(',',$adds);
            foreach($value_2 as $v2)
            {
                echo $v2;

        //insert into tbl_orderdetail-> addon items          
        $addon_detail="INSERT INTO tbl_orderdetail_addon (OrderID,addOns_id,addOns_price) VALUES ('$o_id','$v2',0)";

        mysql_query($addon_detail)or die(mysql_error());
         }
         }


          ?>

如何删除额外的行?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

    <?php
    //Insert order
    $insert="INSERT INTO tbl_order(id,pickup_id,dropoff_id,customer_name,contact_number,remark_by_customer,pay_type,p ickup_date,return_date,renting_days,amount,Create_Dt) VALUES  ('$u_id','$loca_id','$drop_id','$c_name','$contact',' $c_remark','$payment_method','$date_value','$date_value_2','$r_days','$grand_sum',now())";

    mysql_query($insert)or die(mysql_error());
    $o_id=mysql_insert_id();
    foreach($_SESSION['cart'] as $key=>$val)
    {
    if(! empty($val['car_id']))
    {
    $cars=$val['car_id'];
    $o_id=mysql_insert_id();   //retrieve order id
    //insert into tbl_orderdetail->car items      
    $order_details="INSERT INTO tbl_orderdetail (OrderID,carName_id,Car_Unitprice,comment) VALUES ('$o_id','$cars','$grand_sum','$c_remark')";
    mysql_query($order_details)or die(mysql_error());
    }
    }
    $o_id=mysql_insert_id();//retrieve order id
    foreach($_SESSION['cart'] as $key_1=>$val_1)
    {

    $adds=$val_1['addon'];
    $value_2=explode(',',$adds);
    foreach($value_2 as $v2)
    {
    echo $v2;
    if(! empty($v2)
    {
    //insert into tbl_orderdetail-> addon items          
    $addon_detail="INSERT INTO tbl_orderdetail_addon (OrderID,addOns_id,addOns_price)     VALUES ('$o_id','$v2',0)";


    mysql_query($addon_detail)or die(mysql_error());
    }
    }
    }


    ?>