Wednesday 2 October 2013

Mysql; CASE; multiple WHEN ? THEN ?. How to create data for ? with foreach

Mysql; CASE; multiple WHEN ? THEN ?. How to create data for ? with foreach

Created this
$insertData = array();
foreach ($_POST['entry_id'] as $i => $entry_id) {
$when_then .= 'WHEN ? THEN ? ';
$insertData[] = $_POST['entry_id'][$i];
$insertData[] = $_POST['transaction_partner_name'][$i];
$insertData[] = $_POST['entry_id'][$i];
$insertData[] = $_POST['registration_number'][$i];
$insertData[] = $_POST['entry_id'][$i];
$placeholders_for_number_renamed .= '?,';
}
$placeholders_for_number_renamed = rtrim($placeholders_for_number_renamed,
',');
$sql = "
UPDATE 2_1_transactionpartners SET
CompanyName = CASE NumberRenamed
$when_then
END,
RegistrationNumber = CASE NumberRenamed
$when_then
END
WHERE NumberRenamed in ($placeholders_for_number_renamed)";
try {
$stmt = $db->prepare($sql);
$stmt->execute($insertData);
}
But can not match $insertData with ? (must replace ? with
corresponding/necessary $insertData)
qyery is this
UPDATE 2_1_transactionpartners SET
CompanyName = CASE NumberRenamed
WHEN ? THEN ? WHEN ? THEN ?
END,
RegistrationNumber = CASE NumberRenamed
WHEN ? THEN ? WHEN ? THEN ?
END
WHERE NumberRenamed in (?,?)
And array of $insertData is this
Array
(
[0] => 11
[1] => name 2
[2] => 11
[3] => number 2
[4] => 11
[5] => 10
[6] => name 1
[7] => 10
[8] => number 1
[9] => 10
)
Starting from [2] values goes to wrong place. [2] must go to the first ?
in RegistrationNumber = CASE NumberRenamed WHEN ? THEN ? but it (?) goes
to the third ? in CompanyName = CASE NumberRenamed WHEN ? THEN ? WHEN ?
I understand that in foreach must create $insertData in some other order,
but can not understand correct order.
Tried like $entry_id_for_company_name[] =
array_merge($_POST['entry_id'][$i],
$_POST['transaction_partner_name'][$i]); but this is not solution...
No idea at the moment. Please advice

No comments:

Post a Comment