2014-10-20 53 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(); 
} 
相關問題