2

Based on this (How to insert an array into a single MySQL Prepared statement w/ PHP and PDO) information trying to insert multiple rows.

Input (multiple rows)

<input type="text" name="date_day[]">
<input type="text" name="date_day[]">    

<input type="text" name="amount[]">
<input type="text" name="amount[]">

Get values from input

$date_day = $_POST['date_day'];
print_r($date_day);
echo ' date_day with print_r<br>';
$amount = $_POST['amount'];
print_r($amount);
echo ' amount with print_r<br>';

As a result of print_r can see

Array ( [0] => 22 1 => 23 ) date_day with print_r

Array ( [0] => 45 1 => 65 ) amount with print_r

Then from two arrays want to create one array

$data = array_combine($date_day,$amount);

Then insert code

$sql = "INSERT INTO 2_1_journal (TransactionPartnerNameOrDescription, DocumentName) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($data as $row) {
$insertQuery[] = '(?, ?)';
$insertData[] = $amount;
$insertData[] = $row;
}

if (!empty($insertQuery)) {
$sql .= implode(', ', $insertQuery);
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}

As a result in:

column TransactionPartnerNameOrDescription are inserted two rows with word Array

column DocumentName are inserted two rows with 45 and 65 ($amount array)

Please advice why in column TransactionPartnerNameOrDescription instead of array values is inserted word Array?

I suppose it is related with this code

$insertQuery = array();
$insertData = array();
foreach ($data as $row) {
$insertQuery[] = '(?, ?)';
$insertData[] = $amount;
$insertData[] = $row;

but I do not understand what the each line does... may be some link with explanations or similar examples

Latter instead of $data = array_combine($date_day,$amount); used $data = array_merge($date_day,$amount);.

In this case get four rows for column Document name with values 22, 23, 45, 46. And word Array for TransactionPartnerNameOrDescription column

Update Actually things are more simple than seems at first sight

Here is code to insert more values (as sample for someone else; may be will be useful)

$sql = "INSERT INTO 2_1_journal (RecordDay, RecordMonth, RecordYear, Amount) VALUES ";
$insertQuery = array();
$insertData = array();
foreach ($_POST['date_day'] as $i => $date_day) {
$insertQuery[] = '(?, ?, ?, ?)';
$insertData[] = $date_day;
$insertData[] = $_POST['date_month'][$i];
$insertData[] = $_POST['date_year'][$i];
$insertData[] = $_POST['amount'][$i];
}

One more question

Please, advice what means (does)

$insertQuery = array();
$insertData = array();

Does it simply define/set that $insertQuery and $insertData both are arrays?

Something related is here What does $variable ?: [] do? but for me not fully understandable. Please, advice

5
  • rather from this: $data = array_combine($date_day,$amount); Commented May 12, 2013 at 17:38
  • Latter instead of $data = array_combine($date_day,$amount); used $data = array_merge($date_day,$amount);. In this case get four rows for column Document name with values 22, 23, 45, 46. And word Array for TransactionPartnerNameOrDescription column Commented May 12, 2013 at 17:40
  • How come that amount being stored into field named TransactionPartnerNameOrDescription, and date - into DocumentName. Field names looks quite inconsistent Commented May 12, 2013 at 17:43
  • Numbers in table name also smells of some delusion Commented May 12, 2013 at 17:44
  • Field names are because I took example and used for my tables. Table names were already created. I only tried the example to get it work. When will get to work, then will change names Commented May 12, 2013 at 17:50

1 Answer 1

1
foreach ($_POST['date_day'] as $i => $date_day) {
    $insertQuery[] = '(?, ?)';
    $insertData[] = $_POST['amount'][$i];
    $insertData[] = $date_day;
}

Does it simply define/set that $insertQuery and $insertData both are arrays?

Yes.

And [] is a shorthand for array() since PHP 5.4, so you can make it

$insertQuery = [];
$insertData = [];

or

$insertQuery = array();
$insertData = [];

or

$insertQuery = $insertData = [];
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Is some link available with information about the code in answer? As I understand the first ? is bind with $insertData[] = $date_day; and the second ? is bind with $insertData[] = $_POST['amount'][$i]; Actually I need to insert in more columns. Would need to change foreach ($_POST['date_day'] as $i => $date_day) {.... but how....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.