繁体   English   中英

PHP:致命错误:在非对象中调用成员函数appendChild()

[英]PHP: Fatal error: Call to a member function appendChild() on a non-object in

我正在尝试使用用户输入的数据将其存储到XML数据库文件中,以供进一步使用。 但是,由于某种原因,我继续遇到此错误。

错误1,其中idGenerated的输出是对象以外的任何类型

问题代码总结如下,整个PHP文件均位于上下文代码下方。

我尝试使用strval()转换我得到的整数mt_rand()在使用uniqid() mt_rand()之前使用mt_rand() strval() ,还尝试将Type Casting转换为(string) ,甚至尝试了Type Casting转换为(object) 都没有运气。 uniqid()返回一个字符串。 实际上,当我尝试对对象进行类型转换(来自uniqid()mt_rand() )时返回的错误是不同的,并且使生活混乱:

[![错误2,idGenerated的输出是对象] [2]] [2]

为什么哦,为什么createTextNode讨厌字符串,整数和对象? 以及为什么文件中的其他变量(经过SAME进程)通过完全正确?

总结问题代码

$idGenerated = uniqid();

    $dom = new DOMDocument();
    $dom->load('../test.xml');

    $idDec = $dom->createTextNode($idGenerated);

    $id->appendChild($idDec);

    $messageElement->appendChild($id);

整个档案:

<?php
session_start();
//Generate random number to avoid overwrite of file
$randomNumber = mt_rand(10, 99);
move_uploaded_file($_FILES['attachment'][tmp_name], "../uploads/" . $randomNumber . $_FILES['attachment'][name]);

//General Variables
***$idGenerated = uniqid();***
$currentDate = date('l jS \of F h:i:s A');
$sender = $_SESSION['user'];
$message = $_POST['messageText'];
$up_file = $randomNumber . $_FILES['attachment'][name];
$up_file_location = "uploads/" . $up_file;

echo "<br>";
var_dump($currentDate);
echo "<br>";
var_dump($sender);
echo "<br>";
var_dump($message);
echo "<br>";
var_dump($up_file);
echo "<br>";
var_dump($up_file_location);

    $dom = new DOMDocument();
    $dom->load('../test.xml');

    //Dom Variable Declarations
        //Declare String Data
    ***$idDec = $dom->createTextNode($idGenerated);***
    $date = $dom->createTextNode($currentDate);
    $name = $dom->createTextNode($sender);
    $messageText = $dom->createTextNode($message);
    $link = $dom->createTextNode($up_file_location);
    $linkName = $dom->createTextNode($up_file);
        //Declare Data Elements
    $id = $dom->createElement('id');
    $timecode = $dom->createElement('timecode');
    $author = $dom->createElement('author');
    $content = $dom->createElement('content');
    $filePath = $dom->createElement('filePath');
    $fileName = $dom->createElement('fileName');
    $messageElement = $dom->createElement('message');


    //Create XML Data Structure
    //ID
    ***$id->appendChild($idDec);***

    //Date
    $timecode->appendChild($date);

    //Name
    $author->appendChild($name);

    //Message
    $content->appendChild($messageText);

    //File (if there is one!)
    $filePath->appendChild($link);
    $fileName->appendChild($linkName);

    //Message Wrapper Element
    ***$messageElement->appendChild($id);***
    $messageElement->appendChild($timecode);
    $messageElement->appendChild($author);
    $messageElement->appendChild($content);
    $messageElement->appendChild($filePath);
    $messageElement->appendChild($fileName);

    //Load last existing XML entry for reference (which, since they are all in reverse order, will actually be the FIRST entry in the database)
    $xml = simplexml_load_file('../test.xml');
    $lastEntry = $xml->log->message[0];

    //Place new data BEFORE the last existing message Element
    $newEntry = $dom->firstChild->appendChild($messageElement);
    $lastEntry->parentNode->insertBefore($newEntry, $lastEntry);

    $dom->save('../test.xml');

    header("Location: ../index.php");
?>

每当Call to a member function … on a non-object获得Call to a member function … on a non-object ,您就将某种对象用作对象,而该对象在调用时不是对象。 (这经常出现在循环中,在循环中您访问数组元素,并期望当它不是对象时成为对象。)

但是在您的情况下,仅是没有定义$messageElement的事实。 也许您从其他地方复制/粘贴了代码,却忘记了复制$messageElement变量的初始定义/初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM