2014-11-24 47 views
-1

我有一個腳本,它接受表單數據並將其提交給一個XML文件,但是在添加了一些額外的節點後,該腳本返回一個空白頁並且無法寫入XML,甚至沒有返回在最後回聲'添加另一個'。這件事我完全不知所措。在諮詢了我們的服務器供應商之後,一切都看起來很好。有什麼建議麼?PHP SimpleXML輕微更改後失敗

繼承人的原代碼:

include ('resources/header.php');  
$xml = new DOMDocument('1.0', 'utf-8'); 
$xml->formatOutput = true; 
$xml->preserveWhiteSpace = false; 
$xml->load('../products/pending/products_pending.xml'); 

$element = $xml->getElementsByTagName('product')->item(0); 

$name = $element->getElementsByTagName('name')->item(0); 
$description = $element->getElementsByTagName('description')->item(0); 
$price = $element->getElementsByTagName('price')->item(0); 
$our_price = $element->getElementsByTagName('ourprice')->item(0); 
$quantity = $element->getElementsByTagName('quantity')->item(0); 
$size = $element->getElementsByTagName('size')->item(0); 
$color = $element->getElementsByTagName('color')->item(0); 
$vendor = $element->getElementsByTagName('vendor')->item(0); 
$category = $element->getElementsByTagName('category')->item(0); 
$subcategory = $element->getElementsByTagName('subcategory')->item(0); 

$newItem = $xml->createElement('product'); 

$newItem->appendChild($xml->createElement('inventory_type', 'inventory_type')); 
$newItem->appendChild($xml->createElement('model_number', 'model_number')); 
$newItem->appendChild($xml->createElement('sku', $product_sku)); 
$newItem->appendChild($xml->createElement('category', $_POST['product_category'])); 
$newItem->appendChild($xml->createElement('subcategory', $_POST['product_subcategory'])); 
$newItem->appendChild($xml->createElement('description', $_POST['product_description'])); 
$newItem->appendChild($xml->createElement('manufacturer', 'manufacturer')); 
$newItem->appendChild($xml->createElement('barcode_upc', 'barcode_upc')); 
$newItem->appendChild($xml->createElement('color', $_POST['product_color'])); 
$newItem->appendChild($xml->createElement('size', $_POST['product_size'])); 
$newItem->appendChild($xml->createElement('min', 'min')); 
$newItem->appendChild($xml->createElement('max', 'max')); 
$newItem->appendChild($xml->createElement('quantity', $_POST['product_quantity'])); 
$newItem->appendChild($xml->createElement('avg_cost', 'avg_cost')); 
$newItem->appendChild($xml->createElement('freight', 'freight')); 
$newItem->appendChild($xml->createElement('w_cost', '$'.$update_our_price)); 
$newItem->appendChild($xml->createElement('retail', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('our_price', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('min_price', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('p_vendor_number', $update_vendor)); 
$newItem->appendChild($xml->createElement('p_vendor_item_number', $vendor_item_no)); 
$newItem->appendChild($xml->createElement('name', $_POST['product_name'])); 
$newItem->appendChild($xml->createElement('price', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('ourprice', '$'.$update_our_price)); 
$newItem->appendChild($xml->createElement('vendor', $update_vendor)); 
$newItem->appendChild($xml->createElement('consignmentfee', $consignment_fee)); 
$newItem->appendChild($xml->createElement('websiteprice', '$'.$update_website_price)); 
$newItem->appendChild($xml->createElement('image', $image)); 
$newItem->appendChild($xml->createElement('status', 'Pending')); 

$xml->getElementsByTagName('pending_products')->item(0)->appendChild($newItem); 

$xml->save('../products/pending/products_pending.xml'); 

echo '<div class="container content">'; 
echo '<a href="add_product.php">Add Another</a>'; 
include ('resources/footer.php'); 

這裏的新代碼:

include ('resources/header.php'); 

$xml = new DOMDocument('1.0', 'utf-8'); 
$xml->formatOutput = true; 
$xml->preserveWhiteSpace = false; 
$xml->load('../products/pending/products_pending.xml'); 

$element = $xml->getElementsByTagName('product')->item(0); 

$name = $element->getElementsByTagName('name')->item(0); 
$description = $element->getElementsByTagName('description')->item(0); 
$price = $element->getElementsByTagName('price')->item(0); 
$our_price = $element->getElementsByTagName('ourprice')->item(0); 
$quantity = $element->getElementsByTagName('quantity')->item(0); 
$size = $element->getElementsByTagName('size')->item(0); 
$color = $element->getElementsByTagName('color')->item(0); 
$vendor = $element->getElementsByTagName('vendor')->item(0); 
$category = $element->getElementsByTagName('category')->item(0); 
$subcategory = $element->getElementsByTagName('subcategory')->item(0); 

$newItem = $xml->createElement('product'); 

$newItem->appendChild($xml->createElement('inventory_type', 'a')); 
$newItem->appendChild($xml->createElement('model_number', $vendor_item_no)); 
$newItem->appendChild($xml->createElement('sku', $product_sku)); 
$newItem->appendChild($xml->createElement('category', '24')); 
$newItem->appendChild($xml->createElement('subcategory', $_POST['product_category'])); 
$newItem->appendChild($xml->createElement('description', $_POST['product_description'])); 
$newItem->appendChild($xml->createElement('manufacturer', 'manufacturer')); 
$newItem->appendChild($xml->createElement('barcode_upc', $product_sku)); 
$newItem->appendChild($xml->createElement('color', $_POST['product_color'])); 
$newItem->appendChild($xml->createElement('size', $_POST['product_size'])); 
$newItem->appendChild($xml->createElement('lease', 'test')); 
$newItem->appendChild($xml->createElement('book_depr', 'test')); 
$newItem->appendChild($xml->createElement('tax_depr', 'test')); 
$newItem->appendChild($xml->createElement('min', 'test')); 
$newItem->appendChild($xml->createElement('max', 'test')); 
$newItem->appendChild($xml->createElement('quantity', $_POST['product_quantity'])); 
$newItem->appendChild($xml->createElement('avg_cost', 'avg_cost')); 
$newItem->appendChild($xml->createElement('freight', 'freight')); 
$newItem->appendChild($xml->createElement('w_cost', '$'.$update_our_price)); 
$newItem->appendChild($xml->createElement('retail', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('our_price', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('min_price', '$'.$_POST['product_price'])); 
$newItem->appendChild($xml->createElement('price_a', 'test')); 
$newItem->appendChild($xml->createElement('price_b', 'test')); 
$newItem->appendChild($xml->createElement('price_c', 'test')); 
$newItem->appendChild($xml->createElement('spiff', 'test')); 
$newItem->appendChild($xml->createElement('p_vendor_number', $update_vendor)); 
$newItem->appendChild($xml->createElement('p_vendor_item_number', $vendor_item_no)); 
$newItem->appendChild($xml->createElement('p_vendor_last_cost', 'test')); 
$newItem->appendChild($xml->createElement('p_vendor_last_date', 'test')); 
$newItem->appendChild($xml->createElement('s1_vendor_number', 'test')); 
$newItem->appendChild($xml->createElement('s1_vendor_item_number', 'test')); 
$newItem->appendChild($xml->createElement('s1_vendor_last_cost', 'test')); 
$newItem->appendChild($xml->createElement('s1_vendor_last_date', 'test')); 
$newItem->appendChild($xml->createElement('s2_vendor_number', 'test')); 
$newItem->appendChild($xml->createElement('s2_vendor_item_number', 'test')); 
$newItem->appendChild($xml->createElement('s2_vendor_last_cost', 'test')); 
$newItem->appendChild($xml->createElement('s2_vendor_last_date', 'test')); 
$newItem->appendChild($xml->createElement('notes', 'test')); 
$newItem->appendChild($xml->createElement('pos_reminder', 'test')); 
$newItem->appendChild($xml->createElement('invoice_notes', 'test')); 
$newItem->appendChild($xml->createElement('image', $image)); 
$newItem->appendChild($xml->createElement('selection_code', 'test')); 
$newItem->appendChild($xml->createElement('warranty', 'test')); 
$newItem->appendChild($xml->createElement('locator', 'test')); 
$newItem->appendChild($xml->createElement('bar_label', 'test')); 
$newItem->appendChild($xml->createElement('date_in_house', 'test')); 
$newItem->appendChild($xml->createElement('unit', 'test')); 
$newItem->appendChild($xml->createElement('weight', 'test')); 
$newItem->appendChild($xml->createElement('loyalty_exempt', 'test')); 
$newItem->appendChild($xml->createElement('food_stamp', 'test')); 
$newItem->appendChild($xml->createElement('healthcare', 'test')); 
$newItem->appendChild($xml->createElement('scale', 'test')); 
$newItem->appendChild($xml->createElement('status', 'Pending')); 
$newItem->appendChild($xml->createElement('name', $name)); 
$newItem->appendChild($xml->createElement('vendor', $vendor)); 
$newItem->appendChild($xml->createElement('consignmentfee', $consignmentfee)); 
$newItem->appendChild($xml->createElement('websiteprice', $websiteprice)); 

$xml->getElementsByTagName('pending_products')->item(0)->appendChild($newItem); 

$xml->save('../products/pending/products_pending.xml'); 

echo '<div class="container content">'; 
echo '<a href="add_product.php">Add Another</a>'; 
include ('resources/footer.php'); 
+0

有什麼錯在你的Apache的詳細說明'error.log'文件。 – mudasobwa 2014-11-24 18:27:21

回答

0

$element->getElementsByTagName()返回れ對象,但$xml->createElement()詢問兩個字符串。

所以,當你試圖創建一個$name$vendor作爲一個值的元素,你要求PHP將一個DomNode轉換爲一個字符串,但它不能這樣做。你可以在你的日誌文件中找到一些像這樣的錯誤: Catchable fatal error: Object of class DOMElement could not be converted to string on line XX

爲了得到一個字符串,你需要使用nodeValue屬性:

$name = $element->getElementsByTagName('name')->item(0)->nodeValue; 
$description = $element->getElementsByTagName('description')->item(0)->nodeValue; 
$price = $element->getElementsByTagName('price')->item(0)->nodeValue; 
$our_price = $element->getElementsByTagName('ourprice')->item(0)->nodeValue; 
$quantity = $element->getElementsByTagName('quantity')->item(0)->nodeValue; 
$size = $element->getElementsByTagName('size')->item(0)->nodeValue; 
$color = $element->getElementsByTagName('color')->item(0)->nodeValue; 
$vendor = $element->getElementsByTagName('vendor')->item(0)->nodeValue; 
$category = $element->getElementsByTagName('category')->item(0)->nodeValue; 
$subcategory = $element->getElementsByTagName('subcategory')->item(0)->nodeValue;