简体   繁体   English

如何使用PHP在mysql中编写以下查询

[英]How to write following query in mysql using PHP

**My Aim:**Insert the Qty into Db of each I_Code as per each I_Name. **我的目标:**按照每个I_Name将数量插入每个I_Code的Db中。

Expected Output 预期产量

      stock_color
I_Code   I_Qty   I_Name
M1        50      PYC
M2        50      PYC
M1        25      P285C
M3        70      P285C
M4        15      P285C

PHP+Mysql PHP + MYSQL

$ink={M1,M2,M1,M3,M4};
$Qty={50,50,25,70,15};
$ink_name={PYC,P285C};

for($j=0;$j<count($ink_name);$j++)
{
         $sql[] = "insert into stock_color (I_Code,I_Qty,I_Name) values ('$ink[$j]','$Qty[$j]','$ink_name[$j]')"
                foreach ($sql as $query) {
                         mysqli_query($query,$con);
                     }          
}

Now,Code is working but with wrong results.Please help me out.Thanks in advance 现在,代码正在运行,但结果有误。请帮帮我。谢谢

please try this code 请尝试此代码

<?php
 $ink=array('M1','M2','M1','M3','M4');
 $Qty=array(50,50,25,70,15);
 $ink_name=array('PYC','PYC','P285C','P285C','P285C');

 for($j=0;$j<count($ink_name);$j++)
  {
     //$sql .= "insert into stock_color (I_Code,I_Qty,I_Name) values ('$ink[$j]','$Qty[$j]','$ink_name[$j]')";
    if($j==0){
        $sql .= " ('$ink[$j]','$Qty[$j]','$ink_name[$j]')";
    } else {
        $sql .= ", ('$ink[$j]','$Qty[$j]','$ink_name[$j]')";
    }

   }
   echo $final_query = "insert into stock_color (I_Code,I_Qty,I_Name) values " .$sql;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM