2015-09-13 50 views
0

我試圖將一個非常大的表單保存爲一個SQL數據庫作爲XML。 表單是從用戶上傳的XML文件創建的,因此表單的大小未知。除非用戶上傳超過832個元素的文件,否則一切都很好。表單生成的很好,但從表單到XML的輸出只是在第832個元素之後結束。

表單看起來像這樣。此代碼等同於XML文件中的一個元素。

<tr> 
    <td><input type="text" name="115panel" value="1"></td> 
    <td><input type="text" name="115loop" value="1"></td> 
    <td><input type="text" name="115zone" value="1"></td> 
    <td><input type="text" name="115det" value="5"></td> 
    <td><input type="text" name="115type" value="OPT"></td> 
    <td><input type="checkbox" name="115ir" value="1"></td> 
    <td><input type="checkbox" name="115ok" value="1"></td> 
    <td><input type="checkbox" name="115iok" value="1"></td> 
    <td><input type="checkbox" name="115fr" value="1"></td> 
    <td><input type="text" name="115value" value="39"></td> 
</tr> 

$ _POST [name]和$ _POST [type]在窗體的開頭設置。

PHP的代碼來解析形式爲XML看起來像這樣

if($_POST['type'] == "PBS") { 
    $xml = new DOMDocument("1.0"); 
    $root = $xml->createElement("data"); 
    $root->setAttribute('name', $_POST['name']); 
    $root->setAttribute('type', $_POST['type']); 
    $xml->appendChild($root); 
    $i = 0; 
    $iP = 1; 
    $iL = 1; 
    $iD = 1; 
    $run = 1; 
    while($run == 1) { 
     $search = $iP; 
     $search .= $iL; 
     $search .= $iD; 
     if(isset($_POST[$search.'det'])) { 
      $id = $xml->createElement("ID".$i); 
      $root->appendChild($id); 
      $central = $xml->createElement("panel"); 
      $centralText = $xml>createTextNode($_POST[$search.'panel']); 
      $central->appendChild($centralText); 
      $loop = $xml->createElement("loop"); 
      $loopText = $xml->createTextNode($_POST[$search.'loop']); 
      $loop->appendChild($loopText); 
      $zone = $xml->createElement("zone"); 
      $zoneText = $xml->createTextNode($_POST[$search.'zone']); 
      $zone->appendChild($zoneText); 
      $logicnumber = $xml->createElement("det"); 
      $logicnumberText = $xml->createTextNode($_POST[$search.'det']); 
      $logicnumber->appendChild($logicnumberText); 
      $type = $xml->createElement("type"); 
      $typeText = $xml->createTextNode($_POST[$search.'type']); 
      $type->appendChild($typeText); 
      $ir = $xml->createElement("ir"); 
      $irText = $xml->createTextNode(xIt($_POST[$search.'ir'])); 
      $ir->appendChild($irText); 
      $ok = $xml->createElement("ok"); 
      $okText = $xml->createTextNode(xIt($_POST[$search.'ok'])); 
      $ok->appendChild($okText); 
      $iok = $xml->createElement("iok"); 
      $iokText = $xml->createTextNode(xIt($_POST[$search.'iok'])); 
      $iok->appendChild($iokText); 
      $fr = $xml->createElement("fr"); 
      $frText = $xml->createTextNode(xIt($_POST[$search.'fr'])); 
      $fr->appendChild($frText); 
      $value = $xml->createElement("value"); 
      $valueText = $xml->createTextNode($_POST[$search.'value']); 
      $value->appendChild($valueText); 
      $id->appendChild($central); 
      $id->appendChild($loop); 
      $id->appendChild($zone); 
      $id->appendChild($logicnumber); 
      $id->appendChild($type); 
      $id->appendChild($ir); 
      $id->appendChild($ok); 
      $id->appendChild($iok); 
      $id->appendChild($fr); 
      $id->appendChild($value); 
      $i++; 
     } 
     $iD++; 
     if($iD >= 250){ 
      $iD = 1; 
      $iL++; 
     } 
     if($iL >= 9){ 
      $iL = 1; 
      $iP++; 
     } 
     if($iP >= 10){ 
      $iP = 1; 
      $run = 0; 
     } 
    } 
    $xml->formatOutput = true; 
    $mysqli->query("INSERT INTO kl_sks (customerID, user, date, data, status) 
     VALUES ('".addslashes($_POST['name'])."', '".$_SESSION[sesUser]."', '".time()."', '".$xml->saveXML()."', '1')"); 
} 

該代碼通過250 * 8 * 9次循環。在832

這裏總共有18000倍,但輸出停止短路到最後一個的輸出的一個例子。

<ID831> 
    <panel>1</panel> 
    <loop>1</loop> 
    <zone>42</zone> 
    <det>13</det> 
    <type>801PH</type> 
    <ir>0</ir> 
    <ok>0</ok> 
    <iok>0</iok> 
    <fr>0</fr> 
    <value>5</value> 
    </ID831> 
    <ID832> 
    <panel>1</panel> 
    <loop>1</loop> 
    <zone>42</zone> 
    <det>14</det> 
    <type>801PH</type> 
    <ir>0</ir> 
    <ok>0</ok> 
    <iok>0</iok> 
    <fr>0</fr> 
    <value>8</value> 
    </ID832> 
<data> 

這是一個XML文檔有1300元,所以468元丟失

這似乎是一個問題,關於在php.ini中php_value max_input_vars 5000,至極我不能改變。所以問題是如何將一個元素中的所有值輕鬆地組合到一個$ _POST變量中?

+0

可以在你的php.ini –

+0

增加'max_execution_time'標誌的問題I'dont知道如果我能做到這一點。該服務器由one.com託管。 – XerXeX

+0

你可以試試。我不熟悉one.com,所以我在這裏沒有任何幫助。 –

回答

0

考慮以下替代方法有希望解決內存泄漏或可變限制。不再涉及運行while循環,而是遍歷$_POST關聯數組。

我也凝聚你的XML代碼,去掉CreateTextNode對象:

這種方法的
if($_POST['type'] == "PBS") { 
    $dom = new DOMDocument("1.0"); 
    $root = $xml->createElement("data"); 
    $root->setAttribute('name', $_POST['name']); 
    $root->setAttribute('type', $_POST['type']); 
    $dom->appendChild($root); 

    $i = 1; 
    $panel = $loop = $zone = $det = $type = $it = $ok = $iok = $fr = $value = ""; 

    foreach ($_POST as $key => $item){ 

    if (strpos('panel', $key) !== FALSE) { $panel = $item; } 
    if (strpos('loop', $key) !== FALSE) { $loop = $item; } 
    if (strpos('zone', $key) !== FALSE) { $zone = $item; } 
    if (strpos('det', $key) !== FALSE) { $det = $item; } 
    if (strpos('type', $key) !== FALSE) { $type = $item; } 
    if (strpos('it', $key) !== FALSE) { $it = $item; } 
    if (strpos('ok', $key) !== FALSE) { $ok = $item; } 
    if (strpos('iok', $key) !== FALSE) { $iok = $item; } 
    if (strpos('fr', $key) !== FALSE) { $fr = $item; } 
    if (strpos('value', $key) !== FALSE) { $value = $item; } 

    if !empty($panel) && !empty($loop) && !empty($zone) 
     && !empty($det) && !empty($type) && !empty($it) && !empty($ok) 
     && !empty($iok) && !empty($fr) && !empty($value) { 

    $id = $dom->createElement("ID".$i); 
    $root->appendChild($id); 

    $panel = $dom->createElement("panel", $panel); 
    $id->appendChild($panel);    
    $loop = $dom->createElement("loop", $loop); 
    $id->appendChild($loop);    
    $zone = $dom->createElement("zone", $zone) 
    $id->appendChild($zone);    
    $det = $dom->createElement("det", $det); 
    $id->appendChild($det);    
    $type = $dom->createElement("type", $type); 
    $id->appendChild($type);    
    $ir = $dom->createElement("ir", $ir); 
    $id->appendChild($ir);    
    $ok = $dom->createElement("ok", $ok); 
    $id->appendChild($ok);    
    $iok = $dom->createElement("iok", $iok); 
    $id->appendChild($iok);       
    $fr = $dom->createElement("fr", $fr) 
    $id->appendChild($fr);    
    $value = $dom->createElement("value", $value); 
    $id->appendChild($value); 

    $i++;  
    $panel = $loop = $zone = $det = $type = $it = $ok = $iok = $fr = $value = "";  

    $mysqli->query("INSERT INTO kl_sks (customerID, user, date, data, status) 
        VALUES ('".addslashes($_POST['name'])."', '".$_SESSION[sesUser]."', 
          '".time()."', '".$dom->saveXML()."', '1')");  
    } 
    } 
} 

一個需要注意的是,如果任何十個節點物品缺少,等待下一個相應的項目來完成其10批設置,因此錯位行(例如,115ok,115iok,116fr,115值)。在這種情況下,鍵上的附加邏輯可以起到幫助作用

+0

謝謝我會嘗試一下,用一些小的mods。我喜歡foreach循環,不知道爲什麼我沒有這樣做。但是,爲什麼要刪除'textNode'對象並將其替換爲一個'Element'對象?只是我必須在所有其他處理xml文件的代碼中改變它。 – XerXeX