Error editing XML file using PHP form

0

How about, I'm trying to edit an .xml file using PHP and HTML, my problem is that when I edit a node it goes well, then the other nodes are deleted and remain empty. Here my complete code.

Book1.xml file

<?xml version="1.0" encoding="UTF-8"?>
<info>  
    <nombre>Paulo Coelho</nombre>
    <libro>El demonio y la señorita Prym</libro>
    <ano>2000</ano>
    <editorial>Planeta</editorial>
    <servidor>DropBox</servidor>
    <formato>.epub</formato>
</info>

and my editor.php file

<?php

if(isset($_POST['submit'])) {

 $data = simplexml_load_file('libros/libro1.xml');
 $data->nombre=$_POST['nombre'];
 $data->libro=$_POST['libro'];
 $data->año=$_POST['año'];
 $data->editorial=$_POST['editorial'];
 $data->servidor=$_POST['servidor'];
 $data->formato=$_POST['formato'];

$handle=fopen("libros/libro1.xml","wb");
fwrite($handle,$data->asXML());
fclose($handle);
}

$data=simplexml_load_file('libros/libro1.xml');
$info=$data->nombre;
$libro=$data->libro;
$año=$data->año;
$editorial=$data->editorial;
$servidor=$data->servidor;
$formato=$data->formato;

?>

<form action="" method="post">
<input type="text" name="nombre"> Escritor: <?php echo $info; ?>
<br>
<input type="text" name="libro"> Titulo del Libro: <?php echo $libro; ?>
<br>
<input type="text" name="año"> Año de Publicación: <?php echo $año; ?>
<br>
<input type="text" name="editorial"> Editorial: <?php echo $editorial; ?>
<br>
<input type="text" name="servidor"> Servidor: <?php echo $servidor; ?>
<br>
<input type="text" name="formato"> Formato: <?php echo $formato; ?>
<br>
    <input type="submit" name="submit" value="submit">
</form>

My problem is that if I edit the format field adding another value for example: .ebook then it changes it, but the other nodes are empty.

Before editing the book1.xml file

<?xml version="1.0" encoding="UTF-8"?>
<info>  
    <nombre>Paulo Coelho</nombre>
    <libro>El demonio y la señorita Prym</libro>
    <ano>2000</ano>
    <editorial>Planeta</editorial>
    <servidor>DropBox</servidor>
    <formato>.epub</formato>
</info>

After editing the book1.xml file using my form:

<?xml version="1.0" encoding="UTF-8"?>
<info>  
    <nombre></nombre>
    <libro></libro>
    <ano></ano>
    <editorial></editorial>
    <servidor></servidor>
    <formato>.ebook</formato>
</info>

Only the field that I have edited is edited but the others disappear, You could tell me that I am doing wrong, Thank you very much.

    
asked by Joseph Gonzaga 05.07.2017 в 16:29
source

1 answer

1

I give you a simple example in which all the values entered in a variable are saved incrementally, for that you have to pass this variable in the form.

I have done a var_dump of the array so you can see that the values are accumulating, in your case what you have to do is go through an array of values and create the node for each element. All arrays must have the same index, that is, if you do not fill in a form field you add an empty element in the corresponding array. This involves the creation of several nodes .

<?php
$nombres = array();
$libros = array();

if(isset($_POST['submit'])) {

    $nombres = explode(",",$_POST['nombres']);
    $libros = explode(",",$_POST['libros']);
    array_push($nombres,$_POST['nombre']);
    array_push($libros,$_POST['libro']);

    var_dump($nombres); echo "<br>"; var_dump($libros); echo "<br><br>";
}
?>

<form action="" method="post">
<input type="hidden" name="nombres" value="<?php echo implode(",",$nombres);?>">
<input type="hidden" name="libros" value="<?php echo implode(",",$libros);?>">
<label for="nombre">Escritor: </label>
<input type="text" name="nombre"> 
<br>
<label for="libro">Libro: </label>
<input type="text" name="libro">
    <input type="submit" name="submit" value="submit">
</form>

Another thing, if you want the fields to have a previous value, for example the one corresponding to the last node, instead of

<input type="text" name="nombre"> Escritor: <?php echo $info; ?>

Do it with the value attribute:

<input type="text" name="nombre" value="<?php echo $info; ?>">

and so the value is sent in the form.

Keep in mind that this system will only save the information while you are sending the form, if you close the page the information is lost. I recommend again that you save this data in a database and create the xml by consulting the database.

    
answered by 07.07.2017 / 10:22
source