简体   繁体   中英

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.

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

$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;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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