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


PHP: Using 2 arrays in a foreach but one array containing null values

嗨,我是一个PHP新手,但我一直在学习,这是我第一次来Stackoverflow,实际上问了一个问题,所以请对我宽容:p

我正在创建一个工作请求系统,但我被困在一个特定的位,我想我错过了一些很简单的东西,所以我希望你能帮助我。

我正在创建的表单目前有许多复选框(选择)和文本框(数量),采购项目由此查询从数据库中列出

<?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>'n";
} 
?> 

复选框被添加到数组"additem"中,文本框被添加到数组"addquantity"中

提交表单时,复选框&最后执行quantiys,以便它们可以获得MAX(ID)并被索引到另一个表。

这是输入复选框和数量字段的代码。

    <?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);
                                        }       
                                }
                            }
?>

问题是,它的工作…只有被选中的复选框被输入到数组中,但是数量的空文本字段被输入到"addquantity"数组中,所以我试图使用

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

但是它仍然包含NULL字段。因此,一旦提交查询,我就会在一个空间后从我的查询中失去一个数量(对不起,如果我在解释方面很糟糕!!)

这就是查询失败的原因(注意10,11,12,14,我没有检查选项13,所以现在选项14的数量缺失,为了测试目的,我保持id和数量相同)。

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

事先感谢您的帮助。

EDIT: What i changed

我已经改变了sql查询

    $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);
                            }       
                    }

指定键时,没有调用该数量的字符串。

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

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

试试这个怎么样?

将数量输入字段的默认值设置为0,并设置数组索引以匹配您的项目id:

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

然后,使用商品的id作为数组键访问数量数组:

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);
}

一个选项是循环遍历数组'addquantity'来检查空值。当遇到空值时,将其赋值为0。这样,在数据库中插入值时,值就不会为空。