I have this xml that I want to insert some info, the code is working fine but when it comes to the address of the XML I’m having some trouble.
I have a link in other page with:
produtoadicionado.php?page=adicionar&cod_produto=1&id=1&nome_produto=Arroz
What I want is complete the name of the XML with the value of $id before. Which in this case is 1.
So it will call and add in: 1_produtos.xml
But is not working. Is creating a new xml named $id_produtos.xml
produtoadicionado.php
<?php
$page = $_GET["page"];
$cod_produto = $_GET["cod_produto"];
$id = $_GET["id"];
$nome = $_GET["nome_produto"];
if ($page == 'adicionar')
{
$xml = simplexml_load_file('$id_produtos.xml');
$produto = $xml->addChild('produto');
$produto->addChild('nome', $nome);
$produto->addChild('cod', $cod_produto);
file_put_contents('$id_produtos.xml', $xml->asXML());
}
?>
Please help me!
Change these lines:
into this:
Please, note the use of double quote instead of the single quote which let PHP to interpret the vars name and replace that with their value. You can read more here.