I am trying to take data that is entered into a form, and use a post to process it on the same page, which will take all the data and insert it into a table in a db. This is a form that existed on an .asp page which I am trying to convert to php. The form is used for the input of Out of Stock quantities from a store.
Each row looks like this:
Number | Department | Insp Type | Qty | Memo
______________________________________________________________
1 | Bakery And Deli (OOS) | OOS | 1 | george
2 | Produce | OOS | 6 | apples
and so on....
There are rows for ~24 departments. If a new row is added into the database, the form dynamically updates and shows the new row. The only user enterd data is Qty and Memo. The others are pre-populated by the sql query preceding the form. With the current code I have, when I echo my $sqli (insert query) this is what I get:
I also would like to return a count, for the total amount if 'items' that were input into the Qty field on the form.
Any help would be greatly appreciated. I have been trying to figured out how to do this for the past couple of days. I have been searching google left and right in an attempt to figure this out, but I can completely stumped. Thank you in advance for your help.
That's because you create your SQL-statement inside your foreach loop.
I would also suggest using input arrays (like $_POST['txtStoreID'][0]
and so on...) to make things easier.
In your case, I assume the fields in your $_POST
-array are named something like this: txtStoreID_1
.
// First, create array for all results
$res_arr = array();
// Load array with all results
foreach ($_POST as $key=>$value) {
$tmp_name = explode('_',$key,2);
$pri_key = $tmp_name[1]; // number
$sec_key = $tmp_name[0]; // name
if (!is_array($res_arr[$pri_key])) {
$res_arr[$pri_key] = array();
}
// This creates for example: $res_arr['1']['txtStoreID'] containing 'Store_31'
$res_arr[$pri_key][$sec_key] = $value;
}
// Next, create insert statements
foreach ($res_arr as $data) { // Now you get $data['txtStoreID'] etc.
$sqli = "INSERT INTO Inspection_tbl (Store_ID, Dept_ID, Quantity, Memo, User_ID, Date_Time) ";
$sqli .= "Values ('".$data['txtStoreID']."', ";
$sqli .= "'".$data['txtDeptID']."', ";
$sqli .= "'".$data['txtQuantity']."', ";
$sqli .= "'".$data['txtMemo']."', ";
$sqli .= "'".$data['txtUserID']."', ";
$sqli .= "'now()')";
echo $sqli; // Check query
}
Do note that it's only a calculated guess regarding your input fields, though.
Edit:
Aside from the is_array()
change I made, my guess is you'll need the DeptID, and not the text value for the department.
So, the bottom part of the code should be:
// Next, create insert statements
foreach ($res_arr as $deptID=>$data) { // Now you get $data['txtStoreID'] etc.
$sqli = "INSERT INTO Inspection_tbl (Store_ID, Dept_ID, Quantity, Memo, User_ID, Date_Time) ";
$sqli .= "Values ('".$data['txtStoreID']."', ";
$sqli .= "'".$deptID."', ";
$sqli .= "'".$data['txtQuantity']."', ";
$sqli .= "'".$data['txtMemo']."', ";
$sqli .= "'".$data['txtUserID']."', ";
$sqli .= "'now()')";
echo $sqli; // Check query
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments