Using foreach for sql insert from dynamic post array

James

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.

Marty McVry

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to insert mulitple POST value from array using PHP

From Dev

How to insert dynamic post array php?

From Dev

Insert array values with foreach using codeigniter

From Dev

How to get insert values from a table into an array using SQL?

From Dev

Exclude some $_POST from an array made with foreach

From Dev

Unable to insert data from post array in MySQL

From Dev

Multiple INSERT INTO MySQL from $_POST array

From Dev

Insert into array during foreach

From Dev

SQL query in Foreach loop using array variable

From Dev

SQL query in Foreach loop using array variable

From Dev

Php insert/update multple row, using array and not a foreach

From Dev

Using foreach and nested array to insert rows in MySql with PHP

From Dev

Php insert/update multple row, using array and not a foreach

From Dev

Insert using Foreach

From Dev

Insert Into table using foreach

From Dev

Dynamic SQL Insert Statement

From Dev

Insert results from SQL query into php array

From Dev

Using array as a parameter in postgres dynamic sql

From Dev

Remove item from array using foreach - JavaScript

From Dev

Retrieving values from an array using foreach loop

From Dev

Get deltas from an array using foreach in PHP

From Dev

Displaying images from array using a foreach loop

From Dev

SET IDENTITY_INSERT using a linked server on dynamic SQL

From Dev

SQL insert data dynamic column name from another table

From Dev

Insert form $_POST array into a MySQL database using mysqli

From Dev

Laravel 5.2 Insert from select using dynamic content

From Dev

Insert post data as array from grid in database by codeigniter?

From Dev

using $_POST with foreach loop

From Dev

How to pull info from a dynamic dropdown of array to post in another table

Related Related

  1. 1

    How to insert mulitple POST value from array using PHP

  2. 2

    How to insert dynamic post array php?

  3. 3

    Insert array values with foreach using codeigniter

  4. 4

    How to get insert values from a table into an array using SQL?

  5. 5

    Exclude some $_POST from an array made with foreach

  6. 6

    Unable to insert data from post array in MySQL

  7. 7

    Multiple INSERT INTO MySQL from $_POST array

  8. 8

    Insert into array during foreach

  9. 9

    SQL query in Foreach loop using array variable

  10. 10

    SQL query in Foreach loop using array variable

  11. 11

    Php insert/update multple row, using array and not a foreach

  12. 12

    Using foreach and nested array to insert rows in MySql with PHP

  13. 13

    Php insert/update multple row, using array and not a foreach

  14. 14

    Insert using Foreach

  15. 15

    Insert Into table using foreach

  16. 16

    Dynamic SQL Insert Statement

  17. 17

    Insert results from SQL query into php array

  18. 18

    Using array as a parameter in postgres dynamic sql

  19. 19

    Remove item from array using foreach - JavaScript

  20. 20

    Retrieving values from an array using foreach loop

  21. 21

    Get deltas from an array using foreach in PHP

  22. 22

    Displaying images from array using a foreach loop

  23. 23

    SET IDENTITY_INSERT using a linked server on dynamic SQL

  24. 24

    SQL insert data dynamic column name from another table

  25. 25

    Insert form $_POST array into a MySQL database using mysqli

  26. 26

    Laravel 5.2 Insert from select using dynamic content

  27. 27

    Insert post data as array from grid in database by codeigniter?

  28. 28

    using $_POST with foreach loop

  29. 29

    How to pull info from a dynamic dropdown of array to post in another table

HotTag

Archive