-5

出于某种原因,在 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;


}
4

1 回答 1

1

如果我让这段代码运行:

$tmpTblRow = array(
    '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' => '',
);

$sqlToCols = "col, col, col";

$sqll = sprintf(
    "INSERT INTO $sqlToTbl (%s) VALUES ('%s')",
    $sqlToCols,
    implode("','", array_values($tmpTblRow))
);

var_dump($sqll);

我得到结果:

string(183) "INSERT INTO  (col, col, col) VALUES ('','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。这只能意味着一件事:您在调试时没有转储正确的数组。

于 2013-02-04T20:42:42.737 回答