Parsear in PHP an XML that contains the character '

0

Good afternoon everyone.

I have made a php script to parse an XML file but this file in some of its fields contains the character ', which comes into conflict with the PHP code. At the moment I deleted this character from the XML file but I would like it to be stored in the DB.

I do not know if I should modify the XML file, putting something in front of the character so that when I read it the script does not enter into a conflict, or I must put something in the php script to free it. Can you help me?

I leave the php script:

$fichero = "./obras.xml";
if(!$xml = simplexml_load_file($fichero))
{
    echo "No se ha podido cargar el archivo <br>";
} 
else 
{
    echo "El archivo se ha cargado correctamente <br>";
}

include"./conectar.php";
$enlace= conectarse();

function Mayus($variable) {
$variable = strtr(strtoupper($variable),"àèìòùáéíóúçñäëïöü","ÀÈÌÒÙÁÉÍÓÚÇÑÄËÏÖÜ");
return $variable;
}

mysql_query("SET NAMES utf8");

foreach ($xml as $obra)
{
    $obra = (array) $obra;
    $ninv = $obra['ninventario'];

    $ninv = Mayus($ninv);
    $contrato = Mayus($obra['contrato']);
    $desc = Mayus($obra['titulo']); 

    $consulta ="INSERT INTO 'ficha' ('ninv', 'contrato', 'desc') VALUES ('".$ninv."','".$contrato."','".$desc."')"; 

    $resultado = mysql_query($consulta) or die(mysql_error()) ; 

    echo mysql_errno($enlace);
}

The error that occurs if the XML has the character 'is of this style:

  

You have an error in your SQL syntax near '12345678, 123.04, 0,)' at   line 2

Thank you very much.

    
asked by wiki 07.03.2018 в 18:58
source

1 answer

0

you can try addslashes

  

Returns a string with backslashes in front of the characters that   They need to be escaped. These characters are the single quote ('),   double quote "), backslash () and NUL (the NULL byte). PHP: addslashes Manual

$consulta ="INSERT INTO 'ficha' ('ninv', 'contrato', 'desc') VALUES ('".addslashes($ninv)."','".addslashes($contrato)."','".addslashes($desc)."')";

However, I recommend you read How to avoid SQL injection in PHP?

    
answered by 07.03.2018 / 19:26
source