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