$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()中使用循环?
原因是,我输出的输出或值是通过像这样循环生成的。
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 :(得分:0)
通过使用PDO,您可以为要插入的值准备带有变量的查询。现在,您可以使用要插入的值循环一个数组,使用PDO :: bindParam映射它们并在循环内执行查询。
E.g:
$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();
}