PHP:致命错误:在

时间:2015-11-08 21:35:09

标签: php xml database dom

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

Error 1 Where the output of idGenerated is any type other than object

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

我尝试使用mt_rand()转换我得到的整数(我在使用uniqid()之前使用strval()),也尝试将类型转换转换为(string),甚至尝试过类型转换到(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");
?>

1 个答案:

答案 0 :(得分:0)

每当你得到Call to a member function … on a non-object时,你就会使用某些东西作为一个对象,在调用时它不是一个对象。 (这通常出现在你访问数组元素的循环中,当它不是时,它会成为一个对象。)

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