问题
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
回答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 = [];
来源:https://stackoverflow.com/questions/16510141/need-php-pdo-implode-arrays-and-insert-multiple-rows-in-mysql