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 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起