简体   繁体   中英

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

I am trying to use data entered by the user to store it into an XML Database file for use further down the line. However, for some reason I continue to get this error.

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

The problem code is summarised below and the whole PHP file is below that for context.

I have tried converting the integer I got (I was using mt_rand() before using uniqid() ) using strval() , also tried Type Casting to (string) , even tried Type Casting to (object) . All no luck. uniqid() returns a string already. In fact, the error returned when I try Type Casting to object (both from uniqid() and mt_rand() ) was different, and made life confusing:

[![Error 2 Where the output of idGenerated is an object][2]][2]

Why oh why does a createTextNode hate Strings, Integers AND Objects?? And why do the other variables in the file (undergoing the SAME process) pass by absolutely fine?

Summarised Problem Code

$idGenerated = uniqid();

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

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

    $id->appendChild($idDec);

    $messageElement->appendChild($id);

Whole File:

<?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");
?>

Whenever you get Call to a member function … on a non-object , you're using something as an object which at the time of calling is not an object. (This often seen in loops where you access a array element, expecting it to be an object when it's not.)

But in your case, it's simply the fact that $messageElement is not defined. Maybe you copy/pasted the code from somewhere else and forgot to copy the initial definition/initialisation of the $messageElement variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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