Update XML using PHP -


$xml = simplexml_load_file('xml/item.xml');  $xml->item[$_post['number']]->itemname = $_post['name'];  $xml->item[$_post['number']]->description = $_post['description'];  $xml->item[$_post['number']]->price = $_post['price'];  file_put_contents('xml/item.xml', $xml->asxml()); 

i want update xml based on index number coming form number. in code got "php warning: creating default object empty value" error. please advice on this.

it seems haven't checked against existence of $_post['number'] , warning arises value not set, general practice check against variables, add following beginning of code:

if(isset($_post['number'])) { ... 

(consider checking against other variables , function-returns in final production programming practice :)


p.s: actual cause of warning out of range problem (php add node after warning generation). in following code checked existence of node index, , in case of non-existence, added new node (in simple implementation, index of new node not equal $_post['number'], may change behavior suit needs):

if(isset($_post['number'])) {     $xml = simplexml_load_file('xml/item.xml');      if(isset($xml->item[$_post['number']]))         $node = $xml->item[$_post['number']];     else         $node = $xml->addchild('item');      $node->itemname = $_post['name'];     $node->description = $_post['description'];     $node->price = $_post['price'];      file_put_contents('xml/item.xml', $xml->asxml()); } 


disclaimer: error message of same issue in php 5.4.0 - 5.6.8 following (i use v5.5.8):

warning: main(): cannot add element item number 5 when 0 such elements exist in ...

the warning message in original post reads:

warning: creating default object empty value in ...

which same message warning generated when try $xml->item[null] (in php version).


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -