何らかの理由で、PHPでは、implode
関数は返された文字列の最後から余分なフィールドを$tmpTblRow
返し、MySQLエラーを引き起こします。
MySQLステートメントは以下から生成されます:
$sqll = sprintf(
"INSERT INTO $sqlToTbl (%s) VALUES ('%s')",
$sqlToCols,
implode("','", array_values($tmpTblRow))
);
$tmpTblRowは連想配列です。
[lineItem] => null
[partID] => 1
[partNumber] => tr2-mod2-0001
[serialNumber] =>
[partDescription] => mob176wertyu
[quantity] => 10
[price] => 500
[warranty] =>
[dateRequired] => 2055-11-11
[note] =>
[discount] =>
[isProcessed] => 1
[parameter] =>
[isPrivate] =>
[processedByUserID] => 1
[processedDate] => 2013-02-04
[extPrice] => 0
[parentID] => 36
[isToAct] => 1
[userID] => 0
[type] =>
[dateGenerated] => 2013-02-04 10:53:12
[unitType] =>
$ sqll値の戻り値:
INSERT INTO tblOrdersItems
( lineItem, partID , partNumber , serialNumber, partDescription , quantity,
price , warranty , dateRequired , note , discount, isProcessed, parameter,
isPrivate, processedByUserID, processedDate, extPrice, parentID, isToAct,
userID, type, dateGenerated, unitType)
VALUES
( 'null', '1', 'tr2-mod2-0001', '', 'mob176wertyu', '10', '500', '',
'2055-11-11', '', '', '1', '', '', '1', '2013-02-04', '0', '36', '1', '0',
'', '2013-02-04 10:53:12', '', '1' )
返されるエラーは次のとおりです。「列数が行1の値数と一致しません」
1
文字列の最後に余分なものがあることに注意してください$sqll
。誰かがこれを修正する理由と方法を説明できますか?
編集...$tmpTblRowのvar_dumpは以下を返します。
array(23) {
["lineItem"]=>
string(4) "null"
["partID"]=>
string(1) "1"
["partNumber"]=>
string(13) "tr2-mod2-0001"
["serialNumber"]=>
string(0) ""
["partDescription"]=>
string(12) "mob176wertyu"
["quantity"]=>
string(2) "10"
["price"]=>
string(3) "500"
["warranty"]=>
NULL
["dateRequired"]=>
string(10) "2055-11-11"
["note"]=>
NULL
["discount"]=>
NULL
["isProcessed"]=>
string(1) "1"
["parameter"]=>
NULL
["isPrivate"]=>
NULL
["processedByUserID"]=>
string(1) "1"
["processedDate"]=>
string(10) "2013-02-04"
["extPrice"]=>
string(1) "0"
["parentID"]=>
string(2) "36"
["isToAct"]=>
string(1) "1"
["userID"]=>
string(1) "0"
["type"]=>
string(0) ""
["dateGenerated"]=>
string(19) "2013-02-04 10:53:12"
["unitType"]=>
string(0) ""
}
...そして完全なコードはこれです:
$q = "SELECT $sqlFromCols FROM $sqlFromTbl $sqlFromWhere";
$result1 = $conn->query(stripslashes($q)) ;
if (!$result1) die($conn->error.">>none<< 1");
echo $q."||";
echo "<br/>start>";
while($tmpTblRow = $result1->fetch_array(MYSQL_ASSOC) )
{
var_dump ($tmpTblRow);
echo "<br/>end|";
echo "<br/>";
$tmpTblRow[$sqlFromIDcol] = $newID;
$sqll = sprintf("INSERT INTO $sqlToTbl (%s) VALUES ('%s')", $sqlFromCols, implode("','",array_values($tmpTblRow)));
echo $sqll."||";
$result = $conn->query(stripslashes($sqll)) ;
if (!$result) die($conn->error.">>none<< while");
$q = "UPDATE $sqlToTbl SET $change WHERE $sqlToIDcol = $newID";
$q = stripslashes($q);
$result = $conn->query(stripslashes($q)) ;
if (!$result) die($conn->error.">>none<< update");
//mysql_query($q);
//echo $q."||<br>";
$newID = $newID+1;
}