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条)

报告相同问题?

悬赏问题

  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法