【问题标题】:How add quotation marks to each element of the DOMDocument XML to save in MySQL Database如何为 DOMDocument XML 的每个元素添加引号以保存在 MySQL 数据库中
【发布时间】:2018-02-01 15:26:45
【问题描述】:

对不起,英语不是我的母语。

所以,我试图在我的 MySQL 数据库中保存 XML 标记之间的每个元素。一切看起来都不错,但我需要在每个元素中添加“”。看:

XML(如果有帮助的话)

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<layout>
    <encabezado>
        <fecha_reporte> 2017-01-31</fecha_reporte>
        <fecha_esperada>2017-02-28 </fecha_esperada>
    </encabezado>

    <detalle>   
        <matricula>20152999 </matricula>
        <nombre>Ivan </nombre>
        <creditos_costo>1100 </creditos_costo>
        <creditos_cursar>20 </creditos_cursar>
        <creditos_monto> 22000</creditos_monto>
    </detalle>

    <piedepagina>
        <total_estudiantes> 1</total_estudiantes>
        <total_creditos>20 </total_creditos>
        <monto_totalcred>22000 </monto_totalcred>       
    </piedepagina>
</layout>

PHP:

<?php

require ('conexion.php');

$doc = new DOMDocument();
$doc->load('Layout.xml');
$estudiante= $doc->saveXML();

$escapando = mysql_real_escape_string($estudiante);
$query = "INSERT INTO nomina (fecha_reporte, fecha_esperada, matricula, 
nombre, creditos_costo, creditos_cursar, creditos_monto, 
total_estudiante, total_creditos, monto_totalcred) VALUES ('$escapando')";

echo $query;

?>

echo $query; 给了我这一行:INSERT INTO nomina (fecha_reporte, fecha_esperada, matricula, nombre, creditos_costo, creditos_cursar, creditos_monto, total_estudiante, total_creditos, monto_totalcred) VALUES ('\n\n \n 2017-01-31\n 2017-02-28 \n \n \n \n 20152999 \n Ivan \n 1100 \n 20 \n 22000\n \n \n \n 1\n 20 \n 22000 \n \n\n') 如果我删除 mysql_real_escape_string,回显将在没有 \n 的情况下打印,所以我认为我应该删除它。

如何将“”添加到每个元素?例如,像这样: INSERT INTO nomina (fecha_reporte, fecha_esperada, matricula, nombre, creditos_costo, creditos_cursar, creditos_monto, total_estudiante, total_creditos, monto_totalcred) VALUES ('2017-01-31', '2017-02-28', '20152999' 'Ivan', '1100', '20', '22000', '1', '20', '22000')

【问题讨论】:

  • 考虑参数化查询以避免任何引号甚至 MySQL 自己的LOAD XML绝对避免使用已弃用的 mysql* 并在 PHP 7 中停止使用。
  • 为什么不正确解析正在导入的文档?
  • @Parfait 是的,我会更新到 sqli,ty。
  • @NicoHasee 我会调查此事。

标签: php mysql xml domdocument xmldom


【解决方案1】:

如果你做这样的事情,我认为它会起作用

INSERT INTO nomina (fecha_reporte, fecha_esperada, matricula, nombre, creditos_costo, creditos_cursar, creditos_monto, total_estudiante, total_creditos, monto_totalcred) VALUES ('"2017-01-31"', '"2017-02-28"', '"20152999"' '"Ivan"', '"1100"', '"20"', '"22000"', '"1"',   '"20"', '"22000"')

但是,如果您将查询存储在双引号中,就像这样

$sql = "INSERT INTO bla VALUES (blabla)";

那么查询应该是:

INSERT INTO nomina (fecha_reporte, fecha_esperada, matricula, nombre, creditos_costo, creditos_cursar, creditos_monto, total_estudiante, total_creditos, monto_totalcred) VALUES ('\"2017-01-31\"', '\"2017-02-28\"', '\"20152999\"' '\"Ivan\"', '\"1100\"', '\"20\"', '\"22000\"', '\"1\"',   '\"20\"', '\"22000\"')

转义双引号应该可以。

【讨论】:

    猜你喜欢
    • 2011-10-15
    • 2011-10-08
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多