title: Need php pdo implode arrays and insert multiple rows in mysql
body: <p>Based on <a href="https://stackoverflow.com/questions/4629022/how-to-insert-an-array-into-a-single-mysql-prepared-statement-w-php-and-pdo">this</a> (<a href="https://stackoverflow.com/questions/4629022/how-to-insert-an-array-into-a-single-mysql-prepared-statement-w-php-and-pdo">How to insert an array into a single MySQL Prepared statement w/ PHP and PDO</a>) information trying to insert multiple rows.</p>
  
  <p>Input (multiple rows)</p>
  
  <pre><code>&lt;input type="text" name="date_day[]"&gt;
  &lt;input type="text" name="date_day[]"&gt;    
  
  &lt;input type="text" name="amount[]"&gt;
  &lt;input type="text" name="amount[]"&gt;
  </code></pre>
  
  <p>Get values from input</p>
  
  <pre><code>$date_day = $_POST['date_day'];
  print_r($date_day);
  echo ' date_day with print_r&lt;br&gt;';
  $amount = $_POST['amount'];
  print_r($amount);
  echo ' amount with print_r&lt;br&gt;';
  </code></pre>
  
  <p>As a result of print_r can see</p>
  
  <p>Array ( [0] => 22 <a href="https://stackoverflow.com/questions/4629022/how-to-insert-an-array-into-a-single-mysql-prepared-statement-w-php-and-pdo">1</a> => 23 ) date_day with print_r</p>
  
  <p>Array ( [0] => 45 <a href="https://stackoverflow.com/questions/4629022/how-to-insert-an-array-into-a-single-mysql-prepared-statement-w-php-and-pdo">1</a> => 65 ) amount with print_r</p>
  
  <p>Then from two arrays want to create one array</p>
  
  <pre><code>$data = array_combine($date_day,$amount);
  </code></pre>
  
  <p>Then insert code</p>
  
  <pre><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-&gt;prepare($sql);
  $stmt-&gt;execute($insertData);
  }
  </code></pre>
  
  <p>As a result in:</p>
  
  <p>column TransactionPartnerNameOrDescription are inserted two rows with word Array</p>
  
  <p>column DocumentName are inserted two rows with 45 and 65 (<code>$amount</code> array)</p>
  
  <p>Please advice why in column TransactionPartnerNameOrDescription instead of array values is inserted word Array?</p>
  
  <p>I suppose it is related with this code</p>
  
  <pre><code>$insertQuery = array();
  $insertData = array();
  foreach ($data as $row) {
  $insertQuery[] = '(?, ?)';
  $insertData[] = $amount;
  $insertData[] = $row;
  </code></pre>
  
  <p>but I do not understand what the each line does... may be some link with explanations or similar examples</p>
  
  <p>Latter instead of <code>$data = array_combine($date_day,$amount);</code> used <code>$data = array_merge($date_day,$amount);</code>. </p>
  
  <p>In this case get four rows for column Document name with values 22, 23, 45, 46. And word Array for TransactionPartnerNameOrDescription column</p>
  
  <p><strong>Update</strong>
  Actually things are more simple than seems at first sight</p>
  
  <p>Here is code to insert more values (as sample for someone else; may be will be useful)</p>
  
  <pre><code>$sql = "INSERT INTO 2_1_journal (RecordDay, RecordMonth, RecordYear, Amount) VALUES ";
  $insertQuery = array();
  $insertData = array();
  foreach ($_POST['date_day'] as $i =&gt; $date_day) {
  $insertQuery[] = '(?, ?, ?, ?)';
  $insertData[] = $date_day;
  $insertData[] = $_POST['date_month'][$i];
  $insertData[] = $_POST['date_year'][$i];
  $insertData[] = $_POST['amount'][$i];
  }
  </code></pre>
  
  <p><strong>One more question</strong></p>
  
  <p>Please, advice what means (does)</p>
  
  <pre><code>$insertQuery = array();
  $insertData = array();
  </code></pre>
  
  <p>Does it simply define/set that $insertQuery and $insertData both are arrays?</p>
  
  <p>Something related is here <a href="https://stackoverflow.com/questions/16108391/what-does-variable-do">What does $variable ?: [] do?</a> but for me not fully understandable. Please, advice</p>