2014-10-20 49 views
0
$insert_additional_info=sprintf("INSERT INTO tbl_additional_info (productid,size_d,size_d1,bags,cartons,retail_price,sales_price,wholesale_price) VALUES (%s, %s,%s, %s,%s,%s,%s,%s)", 
         GetSQLValueString($product_id, "int"), 
         GetSQLValueString($size_d, "int"), 
         GetSQLValueString($size_d1, "int"), 
         GetSQLValueString($bags, "int"), 
         GetSQLValueString($cartons, "int"), 
         GetSQLValueString($retail_price, "int"), 
         GetSQLValueString($sales_price, "int"), 
         GetSQLValueString($wholesale_price, "int")); 

而不是像上面插入,有没有一种方法,我可以在VALUES()内使用循环?可以在mysql查询中使用php for循环来插入数据吗?

原因是,我的输出或插入值是通过像这样循环生成的。

if(isset($_POST['submit'])) 
{ 

$_SESSION['myInputs_all'] = array($_POST["myInputs_d"],$_POST["myInputs_d1"],$_POST["myInputs_bags"],$_POST["myInputs_carton"],$_POST["myInputs_retail"],$_POST["myInputs_sales"],$_POST["myInputs_wholesale"]); 

$counter = count($_POST['myInputs_d']); 
$column_array=array('productid,size_d,size_d1,bags,cartons'); 
for($c = 0; $c < $counter; $c++) 
    { 
     foreach($myInputs_all as $all_inputs=>$value) 
     { 
      //This is the output 
      echo $value[$c]; 
     } 
} 
} 

现在,我该如何插入echo $ value [$ c];在INSERT QUERY的VALUES内部?

我试过下面的东西,但它说Query是空的。但这就是我想要解决的问题。

$insert_additional_info="INSERT INTO tbl_additional_info (productid,size_d,size_d1,bags,cartons) VALUES("; 
    $counter = count($_POST['myInputs_d']); 

    for($c = 0; $c < $counter; $c++) 
    { 
     foreach($myInputs_all as $all_inputs=>$value) 
     { 
    $insert_additional_info.=$value[$c]; 
    $insert_additional_info.=")"; 
     } 
} 
+0

@ Arif_suhail_123,我这样做,但不是仅仅循环列它,通过行循环以及 – 2014-10-20 09:12:52

+0

@ Arif_suhail_123,我不是很确定你如何想用的爆炸。看看编辑部分,这就是我想实现的。谢谢 – 2014-10-20 09:42:42

+0

@ Arif_suhail_123,好的..没问题.. – 2014-10-20 09:57:53

回答

0

通过使用PDO,您可以使用变量准备要插入的值的查询。现在你可以用你想要插入的值循环一个数组,并用PDO :: bindParam映射它们并在Loop中执行查询。

例如为:

$stmt=$handler->prepare("INSERT INTO tbl_additional_info 
(productid,size_d,size_d1,bags,cartons,retail_price,sales_price,wholesale_price) 
VALUES (var1, var2,var3,var4,var5,var6,var7,var8"); 
foreach($Array as $data){ 
$stmt->bindParam(":var1",$dara["value1"]); 
$stmt->bindParam(":var2",$dara["value2"]); 
$stmt->bindParam(":var3",$dara["value3"]); 
.... 
$stmt->bindParam(":var8",$dara["value8"]); 
$stmt->execute(); 
} 
相关问题