dongqie2010 2016-12-23 08:56
浏览 43

检索多个数据并使用数组和foreach插入多个数据[重复]

This question already has an answer here:

enter image description here

I am listing product list from products table which i select product_group on previous page. In this list, i show product names and empty input in every row of table. Into the empty inputs, manually i write number of products which i count in my shop. After filling all the products numbers (rows in this html table) i want to insert them into another mysql table row by row for each product.

Here is my html table codes:

<table class="table table-responsive" width="100%" border="1">
<tbody>
<tr style="background-color:#C2E17C">
  <td align="center" width="80%"> Stok İsmi</td>
  <td align="center" width="20%"> Sayım Miktarı</td>
</tr>
<?php
 $personelcek = mysql_query("SELECT * FROM stoklar WHERE sto_durum='acik' AND sto_altgrup_kod = '".$_REQUEST['sta_RECno']."' ORDER BY sto_isim ASC");
           while ($a= mysql_fetch_array($personelcek)){
                $sto_RECno  = $a['sto_RECno'];
                $sto_isim   = $a['sto_isim'];
                echo' 
            <tr>
<td align="left" width="80%"><input class="input1" type="hidden" name="stok_kodu[]" value="'.$sto_RECno.'" readonly>'.$sto_isim.'</td>
<td width="20%"><input style="text-align:right" class="input1" type="text" name="sayim_miktari[]" value=""></td>
            </tr>';}?>

</table> 

And also there is one more input outside of the table which i retrieve the product_group_id as this:

<input type="hidden" name="alt_grup_kod[]" value="<?php echo $_REQUEST['sta_RECno']; ?>">

And the last thing, i want to retrieve existing count numbers from mysql products table for each product.

As a summary, i want to insert every product one by one into another mysql table including product_group_id, product_id, existing_count_numbers and manually written numbers.

I tried like this but everytime i get errors:

$stok_kodu          = $_POST['stok_kodu'];
$stoklar            = array($stok_kodu);
$sayim_miktari      = $_POST['sayim_miktari'];
$sayim_miktarlari   = array($sayim_miktari);
$alt_grup_kod       = $_POST['alt_grup_kod'];
$alt_gruplar        = array($alt_grup_kod);
$stok_miktarlari    = array();

$miktar_cek = mysql_query("SELECT * FROM stoklar WHERE sto_RECno = '".$stok_kodu."' ORDER BY sto_RECno");
        while ($miktar_al = mysql_fetch_assoc($miktar_cek)){
            $stok_miktari[] = $miktar_al['sto_miktar'];


foreach ($stok_kodu as $stoklar){
foreach ($sayim_miktari as $sayim_miktarlari){
foreach ($alt_grup_kod as $alt_gruplar){
foreach ($stok_miktari as $stok_miktarlari){


$sayim_kaydet = mysql_query("INSERT INTO sayim_sonucu (alt_grup_kod, stok_kodu, sayim_miktari, stok_miktari) VALUES ('$alt_gruplar', '$stoklar', '$sayim_miktarlari', '$stok_miktarlari')");
}}}}}

Any idea how i can tidy all these codes and insert every product into another table of mysql database?

</div>
  • 写回答

1条回答 默认 最新

  • douzhang7603 2016-12-23 09:37
    关注

    Your foreach makes the trouble.Change your foreach like this. hope this will help.And also an added note 'you definitely needed to write a function to escape mysql injection for every post variable.

    $count=0;
    foreach ($stok_kodu as $stoklar){
    
    $sayim_kaydet = mysql_query("INSERT INTO sayim_sonucu 
    (alt_grup_kod, stok_kodu, sayim_miktari, stok_miktari) VALUES 
    ('$alt_grup_kod[$count]', '$stoklar', 
    '$sayim_miktari[$count]', '$stok_miktari[$count]')");
    
    $count++;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line