dtpa98038 2014-10-20 09:05
浏览 103

可以在mysql查询中使用php for循环来插入数据吗?

$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"));

Instead of INSERTING like above, is there a way that I can use loop inside the VALUES()?

The reason is , my output or value to be inserted generated by looping like this.

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];
        }
 }
}

Now, how do I insert echo $value[$c]; inside the VALUES for the INSERT QUERY?

I tried something like below but it says Query is empty. But this is how I want to solve it.

$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.=")"; 
        }  
 }
  • 写回答

1条回答 默认 最新

  • dtm37893 2014-10-20 09:38
    关注

    By using PDO, you can prepare the Query with variables for the values you want to insert. Now you can loop trough an array with the values you want to insert, an map them with PDO::bindParam and execute the query inside the Loop.

    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();
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog