为所有记录插入相同的id,并重复与foreach循环一样多次

时间:2014-08-08 02:01:50

标签: php foreach

{
............
............
$order_details="INSERT INTO .....";
mysql_query($order_details);
$o_id_detail[]=mysql_insert_id();
}

$prev_id=implode(',',$o_id_detail);
$last_id=explode(',',$prev_id);
foreach($last_id as $last)
{
    $last;
    foreach($addon_price as $a_p=>$p)
    { 
        echo $a_p;
        foreach($p as $m)
        {

            $addon_id= $m['id'];
            echo $m['addon_name'];
            echo $m['deposit'];
            echo $m['ppd'];
            echo $m['pp_eight'];
            echo $m['pp_six'];

            $addon_total = $m['deposit']+$m['ppd']+$m['pp_eight']+$m['pp_six'];
            echo $addon_total;

            $addon_detail="INSERT INTO tbl_orderdetail_addon 
                                (OrderID,addOns_id,addOns_price) 
                            VALUES ('$last','$addon_id','$addon_total')";
            if(!empty($addon_id)){
                mysql_query($addon_detail)or die(mysql_error());
            }
        }     
   }   
}
}

现在,我得到($ last)多次插入foreach循环(foreach($ addon_price as $ a_p => $ p))。 如果特定addon_item的($ last)id已经存在,如何限制($ last)id表单再次插入任何行?

编辑部分:

$o_id_detail=mysql_insert_id();

    foreach($addon_price as $a_p=>$p)
               { 
                echo $a_p;
                   foreach($p as $m)
                   {

                       $addon_id= $m['id'];
                       echo $m['addon_name'];
                       echo $m['deposit'];
                       echo $m['ppd'];
                       echo $m['pp_eight'];
                       echo $m['pp_six'];
                  $addon_total=$m['deposit']+$m['ppd']+$m['pp_eight']+$m['pp_six'];
                   echo $addon_total;




$addon_detail="INSERT INTO tbl_orderdetail_addon (OrderID,addOns_id,addOns_price) VALUES ('$o_id_detail','$addon_id','$addon_total')";
if(!empty($addon_id))
                {
                mysql_query($addon_detail)or die(mysql_error());


              }
            }     
         }   
}

完整代码:

 $user="INSERT INTO tbl_users (email,name,contact,register_date) VALUES ('$email','$c_name','$contact',NOW())";
  mysql_query($user)or die(mysql_error());

             $u_id=mysql_insert_id();
             }
              else
             {
                 $u_id=$user_id;
             }
             $insert="INSERT INTO tbl_order(id,customer_name,contact_number,remark_by_customer,pay_type,amount,Create_Dt) VALUES ('$u_id','$c_name','$contact',' $c_remark','$payment_method','$grand_sum',now())";

mysql_query($insert)or die(mysql_error());

             //tbl_orderdetail
             //getting car ids
             // $car_list= implode(',',$cars);
             $o_id=mysql_insert_id();
             foreach($_SESSION['cart'] as $key=>$val)
             {
                 $unit=strip_tags($key);
                 $new_pickup=$val['location'];
                 $new_dropoff=$val['dropoff'];
                 $cars=$val['car_id'];

                 //pickup table
                  mysql_select_db($database_bumi_conn, $bumi_conn);
                  $query_orderdetail="SELECT * FROM tbl_pickup_location WHERE pickup_location='$new_pickup'";
                  $result_orderdetail=mysql_query($query_orderdetail)or die(mysql_error());
                  $row_orderdetail=mysql_fetch_array($result_orderdetail);
                  $new_pick_id=$row_orderdetail['pickup_id'];

                  //dropoff table
                   mysql_select_db($database_bumi_conn, $bumi_conn);
                  $query_orderdetail_2="SELECT * FROM tbl_dropoff WHERE dropoff_location='$new_dropoff'";
                  $result_orderdetail_2=mysql_query($query_orderdetail_2)or die(mysql_error());
                  $row_orderdetail_2=mysql_fetch_array($result_orderdetail_2);
                  $new_dropoff_id=$row_orderdetail_2['dropoff_id'];



            $new_date_1=$val['date_value'];
            $new_date_2=$val['date_value_2'];
            $total_renting_days=$val['total_days'];




      $order_details="INSERT INTO tbl_orderdetail (OrderID,pickup_id,dropoff_id,pickup_date,return_date,renting_days,carName_id,Car_Unitprice) VALUES ('$o_id','$new_pick_id',' $new_dropoff_id','$new_date_1','$new_date_2','$total_renting_days','$cars','$unit')";
if(!empty($cars)&&($unit)){
  mysql_query($order_details)or die(mysql_error());
      $o_id_detail=mysql_insert_id();
      echo '>>>'.$o_id_detail.'<<<';
             }}


            foreach($addon_price as $a_p=>$p)
                                   { 
                                    echo $a_p;
                                       foreach($p as $m)
                                       {

                                           $addon_id= $m['id'];
                                           echo $m['addon_name'];
                                           echo $m['deposit'];
                                           echo $m['ppd'];
                                           echo $m['pp_eight'];
                                           echo $m['pp_six'];
                                      $addon_total=$m['deposit']+$m['ppd']+$m['pp_eight']+$m['pp_six'];
                                       echo $addon_total;




            $addon_detail="INSERT INTO tbl_orderdetail_addon (OrderID,addOns_id,addOns_price) VALUES ('$o_id_detail','$addon_id','$addon_total')";
               if(!empty($addon_id)){
  mysql_query($addon_detail)or die(mysql_error());


                                  }
                                              }     
                                   }   
             } // foreach($addon_price as $a_p=>$p)

1 个答案:

答案 0 :(得分:0)

(OrderID, addOns_id)上向表格添加唯一的综合索引。然后使用

$addon_detail="INSERT IGNORE INTO tbl_orderdetail_addon 
                    (OrderID,addOns_id,addOns_price) 
                VALUES ('$last','$addon_id','$addon_total')";

IGNORE选项意味着如果有重复的密钥,它将默默跳过插入尝试。