Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
rather from this: $data = array_combine($date_day,$amount); –  Your Common Sense May 12 '13 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 –  user2360838 May 12 '13 at 17:40
 
How come that amount being stored into field named TransactionPartnerNameOrDescription, and date - into DocumentName. Field names looks quite inconsistent –  Your Common Sense May 12 '13 at 17:43
 
Numbers in table name also smells of some delusion –  Your Common Sense May 12 '13 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 –  user2360838 May 12 '13 at 17:50
add comment

1 Answer

up vote 1 down vote accepted
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 = [];
share|improve this answer
 
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.... –  user2360838 May 12 '13 at 17:48
 
dunno about links. may be php.net/foreach –  Your Common Sense May 12 '13 at 17:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.