PHP does not save message in XML

Asked

Viewed 48 times

1

I want to set up a comment system, when opening the xml php can, but when saving a new message not, and can not save it also not from the error. (Note: it is kind of urgent pro TCC). Codes:

pag_perfilAbertoTec.php:

    // isso está dentro de uma div.
        $tipoUsuario = pegaTipoUsuario($conexao, $cpf);
        $dom = new DOMDocument("1.0", "ISO-8859-7");
        $root = lerXML($dom, $conexao, $cpfTec, $cpf, $tipoUsuario);
        $dom = $_SESSION['dom'];
        $ver = simplexml_import_dom($dom);
        foreach ($ver as $xml):
            if ($xml->cpf_origem == 1234) {
                echo '<p align="center">' . $xml->mensagem . '</p>';
            } else if ($xml->cpf_origem == $cpf) {
                echo '<p align="right">' . $xml->mensagem . '</p>';
                echo '<p align="right">' . $xml->data_envio . '</p>';
            } else {
                echo '<p align="left">' . $xml->mensagem . '</p>';
                echo '<p align="left">' . $xml->data_envio . '</p>';
            }
        endforeach;
    // Sim, está vindo post..

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $mensagem = $_POST['txtMsg'];
        $msg = addMensagem($dom, $mensagem, $cpf);
        $root->appendChild($msg);
        $dom->appendChild($root);
        # Para salvar o arquivo, descomente a linha
        $dom->save("contatos.xml");
        unset($_POST['txtMsg']);
        // estou usando js aqui porque o header está dando erro..
        echo '<script> window.location("pag_perfilAbertoTec.php"); </script>';
    }

php database.

function addMensagem($documento, $mensagem, $cpf_origem) {
    // criar msg
    $msg = $documento->createElement("msg");
    // criar nó data
    $data = date('j/n/Y H:i:s');
    $dataElm = $documento->createElement("data_envio", $data);
    // criar nó origem
    $cpf_origemElm = $documento->createElement("cpf_origem", $cpf_origem);
    // criar nó mensagem (texto)
    $mensagemElm = $documento->createElement("mensagem", $mensagem);

    $msg->appendChild($dataElm);
    $msg->appendChild($cpf_origemElm);
    $msg->appendChild($mensagemElm);
    return $msg;
}
//daqui vem o $root
function lerXML($dom, $conexao, $cpfCli, $cpfTec, $tipo) {
        if ($tipo === 2) {
            $sql = "select caminho_xml as xml from tbl_mensagem where cpf_cliente_fk = $cpfCli and cpf_tecnico_fk = $cpfTec";
        } else {
            $sql = "select caminho_xml as xml from tbl_mensagem where cpf_cliente_fk = $cpfTec and cpf_tecnico_fk = $cpfCli";
        }
        $return = mysqli_query($conexao, $sql);
        $row = mysqli_fetch_array($return, MYSQLI_ASSOC);
        $nomeArquivo = $row["xml"];
        if (!file_exists($nomeArquivo)) {
            $novonome = gerarNomeXML();
            guardarXML($conexao, $novonome, $cpfCli, $cpfTec, $tipo);
            $msg = addMensagem($dom, "Conectados!", null);
            // criando nó principal
            $root = $dom->createElement("mensagens");
            // adiciona a mensagem ao root
            $root->appendChild($msg);
            // adiciona o root ao xml
            $dom->appendChild($root);
            // retirar os espaços em branco
            $dom->preserveWhiteSpace = false;
            // gerar código ??
            $dom->formatOutput = true;
            $_SESSION['nomeXML'] = $novonome;
            $dom->save($novonome);
            $_SESSION['dom'] = $dom;
            return $root;
        } else {
            // carrega o arquivo
            $dom->load($nomeArquivo);
            $_SESSION['nomeXML'] = $nomeArquivo;
            // recupera nó principal
            $root = $dom->documentElement;
            $_SESSION['dom'] = $dom;
            return $root;
        }
    }

 function guardarXML($conexao, $caminho, $cpfCli, $cpfTec, $tipo) {
if ($tipo == 2) {
    $sql = "update tbl_mensagem set caminho_xml = '$caminho' where cpf_cliente_fk = $cpfTec and cpf_tecnico_fk = $cpfCli";
} else {
    $sql = "update tbl_mensagem set caminho_xml = '$caminho' where cpf_cliente_fk = $cpfCli and cpf_tecnico_fk = $cpfTec";
}
$resultado = mysqli_query($conexao, $sql);
return $resultado;
}
  • What is $root? What is the assignment for $root, like you did in the code to fill it?

  • I edited and put the lerXML function, where the $root

  • You don’t have to $dom->appendChild($root);; the $root is already part of the $dom!

  • I took the $dom->appendchild($root); but it continued the same, what I can do?

  • Drew $dom->appendChild($root); where? pag_perfilAbertoTec.php?

  • Yeah, from over there.

  • You are saving in the same way as the read xml file, in the file system?

  • 1

    That was the mistake, I think that at the time of implementing the code in my project, I forgot to change the path where the file is saved. Thank you for your attention!

  • 1

    Then you have another problem, the function is returning data and you are not using the return. You are calling the function guardarXML($conexao, $novonome, $cpfCli, $cpfTec, $tipo); the correct would be $variavel = guardarXML($conexao, $novonome, $cpfCli, $cpfTec, $tipo);

  • In the case there you are saying to check if the function has stored the name of the file in the bank, right? (Because if this is the case to get the name of the file, I already have access to that name on a line before). Changing the path that the file is saved, I was able to solve the problem, but another one appeared, in case php does not send the message that the user typed, but another one. I sent: "aa", the page reloaded and did not appear, when sending "ab", appeared the "aa", and so on, some idea of what might be?

  • Would have to know how you’re calling the function lerXML

  • I edited and put the part that reads the XML when the page is reloaded. (It is in the same order as my file, I tried to reverse but gave error..)

Show 7 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.