dpzo13732 2014-03-12 06:43
浏览 71
已采纳

PHP:在foreach中使用2个数组,但是一个数组包含空值

Hi I am quite novice at PHP but i have been learning as i'm going and this is the first time i have come on Stackoverflow and actually asked a question so please go easy on me :P

I am creating a work request system but i am stuck on one particular bit, I think i am missing something quite simple so i hope you can help me.

The form i'm creating currently has a number of check boxes (Selection) and textboxes (Quantity) and the procurement items are listed from a database by this query

<?php 
$query="SELECT * FROM RequestProcurementItems ORDER BY ITEMID"; 
$result = odbc_exec($connection_id,$query);

$requestitems=""; 

while ($row = odbc_fetch_array($result)) { 

    $itemid=odbc_result($result,"ITEMID"); 
    $itemtype=odbc_result($result,"ITEMTYPE");
    $itemdesc=odbc_result($result,"ITEMDESCRIPTION");   

    foreach($row as $cell)
    echo "<tr>";

    echo "<td colspan='2'>". $itemid ." - ". $itemtype ." - ". $cell ."</td><td colspan='2'><input type='checkbox' name='additem[]' value='".$itemid."'><input type='text' name='addquantity[]'>";

    echo "</td>";
    echo "</tr>
";
} 
?> 

The checkboxes are added to the array "additem" and the textboxes are added to the array "addquantity"

When the form is submitted the checkboxes & quantitys are executed last so that they can get the MAX(ID) and be indexed to another table.

This is the code for entering the checkboxes and quantity fields.

    <?php 
$sqlgetmaxid = "SELECT MAX(RequestID) FROM RequestFormProcurement";
            $resultmaxid = odbc_exec($connection_id,$sqlgetmaxid);
            while( $row = odbc_fetch_array($resultmaxid) ) { 

            $qarray = array_filter($_POST['addquantity']);

            // Query to Add Items selected from list.                       
                if(!empty($_POST['additem'])){



foreach($_POST['additem'] as $itemid => $quantity) {
                                        //echo $_POST['additem'][$itemid].$_POST['addquantity'][$itemid];

                                        $sql = "INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (".implode(",",$row).", ".$_POST['additem'][$itemid].", ".$qarray[$itemid].")";
                                        print_r($sql);
                                        odbc_exec($connection_id,$sql);
                                        odbc_commit($connection_id) or die (comm_error);
                                        }       
                                }
                            }
?>

The issue is that it works... only checkboxes that are checked are being entered into the array, but the empty textfields for quantity are being entered into the "addquantity" array so i tried to clean it using

$qarray = array_filter($_POST['addquantity']);

But it still contains NULL fields. So once submitting the query i am losing a quantity from my query after a space (sorry if i'm terrible at explaining!!)

this is how the query is failing (notice the 10,11,12,14 i did not check option 13 so now the quantity for option 14 is missing, i kept id and quantity the same for testing purposes).

INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 10, 10)
INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 11, 11)
INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 12, 12)

Notice: Undefined offset: 3 in I:\IT\Web_Request_Forms_Database\UwAmp\www\includes\HWS\NewHWSWRequestForm.php on line 282
INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 14, )

Many thanks for any help in advance.

EDIT: What i changed

I've changed the sql query

    $sqlgetmaxid = "SELECT MAX(RequestID) FROM RequestFormProcurement";
    $resultmaxid = odbc_exec($connection_id,$sqlgetmaxid);
    while( $row = odbc_fetch_array($resultmaxid) ) { 

    print_r($_POST['addquantity']);
    var_dump($_POST['addquantity']);

    // Query to Add Items selected from list.                       
        if(!empty($_POST['additem'])){

                            foreach($_POST['additem'] as $itemid => $quantity) {
                            //echo $_POST['additem'][$itemid].$_POST['addquantity'][$itemid];

                            $sql = "INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (".implode(",",$row).", ".$_POST['additem'][$itemid].", '".$_POST['addquantity'][$quantity]."')";
                            print_r($sql);
                            odbc_exec($connection_id,$sql);
                            odbc_commit($connection_id) or die (comm_error);
                            }       
                    }

When we specified the key we did not call the string for the quantity.

$sql = "INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (".implode(",",$row).", ".$_POST['additem'][$itemid].", '".**$_POST['addquantity'][$quantity].**"')";

Output

Array ( [10] => 100 [11] => 111 [12] => 0 [13] => 1333 [14] => 0 [15] => 0 [16] => 0 [17] => 177 [18] => 0 [19] => 0 [20] => 20 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 )
array (size=18)
  10 => string '100' (length=3)
  11 => string '111' (length=3)
  12 => string '0' (length=1)
  13 => string '1333' (length=4)
  14 => string '0' (length=1)
  15 => string '0' (length=1)
  16 => string '0' (length=1)
  17 => string '177' (length=3)
  18 => string '0' (length=1)
  19 => string '0' (length=1)
  20 => string '20' (length=2)
  21 => string '0' (length=1)
  22 => string '0' (length=1)
  23 => string '0' (length=1)
  24 => string '0' (length=1)
  25 => string '0' (length=1)
  26 => string '0' (length=1)
  27 => string '0' (length=1)
INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 10, '100')INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 11, '111')INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 13, '1333')INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 17, '177')INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (1001, 20, '20')
  • 写回答

2条回答 默认 最新

  • doukuipei9938 2014-03-12 07:39
    关注

    How about trying something like this?

    Set default value of the quantity input field to 0 and set the array index to match your item's id:

    <input type='text' name='addquantity[".$itemid."]' value='0'>
    

    Then, access the quantity array using your item's id as the array key:

    foreach($_POST['additem'] as $itemid => $quantity) {
        //echo $_POST['additem'][$itemid].$_POST['addquantity'][$itemid];
    
        $sql = "INSERT INTO ProcurementDetail (RequestID, ITEMID, Quantity) VALUES (".implode(",",$row).", ".$_POST['additem'][$itemid].", ".$_POST['addquantity'][$itemid].")";
        print_r($sql);
        odbc_exec($connection_id,$sql);
        odbc_commit($connection_id) or die (comm_error);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计