繁体   English   中英

无法获得正确的想法

[英]Having trouble getting the right idea

好吧,我正在编写一个php代码来编辑这些标签内的标签和数据,但我在解决问题时遇到了很大的麻烦。

基本上我有一个与此类似的xml文件,但是更大

<users>
 <user1>
  <password></password>
  </user1>
</users>

和我用来尝试和更改user1标签的php代码是这个

function mod_user() {
    // Get global Variables
    global $access_level;

    // Pull the data from the form
    $entered_new_username = $_POST['mod_user_new_username'];
    $entered_pass = $_POST['mod_user_new_password'];
    $entered_confirm_pass = $_POST['mod_user_confirm_new_password'];
    $entered_new_roll = $_POST['mod_user_new_roll'];
    $entered_new_access_level = $_POST['mod_user_new_access_level'];

    // Grab the old username from the last page as well so we know who we are looking for
    $current_username = $_POST['mod_user_old_username'];

    // !!-------- First thing is first. we need to run checks to make sure that this operation can be completed ----------------!!

    //  Check to see if the user exist. we just use the normal phaser since we are only reading and it's much easier to make loop through
    $xml = simplexml_load_file('../users/users.xml');

    // read the xml file find the user to be modified
    foreach ($xml->children() as $xml_user_get)
    {
        $xml_user = ($xml_user_get->getName());

        if ($xml_user == $entered_new_username){
            // Set array to send data back
            //$a = array ("error"=>103, "entered_user"=>$new_user, "entered_roll"=>$new_roll, "entered_access"=>$new_access_level);

            // Add to session to be sent back to other page
            // $_SESSION['add_error'] = $a;
            die("Username Already exist - Pass");
            // header('location: ../admin.php?page=usermanage&task=adduser');
        }
    }

    // Check the passwords and make sure they match
    if ($entered_pass == $entered_confirm_pass) {
        // Encrypt the new password and unset the old password variables so they don't stay in memory un-encrytped
        $new_password = hash('sha512', $entered_pass);

        unset ($entered_pass, $entered_confirm_pass, $_POST['mod_user_new_password'], $_POST['mod_user_confirm_pass']);
    }

    else {

        die("passwords did not match - Pass");
    }

    if ($entered_new_access_level != "") {
        if ($entered_new_access_level < $access_level){
            die("Access level is not sufficiant to grant access - Pass");
        }
    }

    // Now to load up the xml file and commit changes.
    $doc = new DOMDocument;
    $doc->formatOutput = true;
    $doc->perserveWhiteSpace = false;
    $doc->load('../users/users.xml');

    $old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0);

    // For initial debugging - to be deleted
    if ($old_user == $current_username)
        echo    "old username found and matches";

    // Check the variables to see if there is something to change in the data.
    if ($entered_new_username != "") {
        $xml_old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0)->replaceChild($entered_new_username, $old_user);

        echo "Username is now: " . $current_username;
    }

    if ($new_pass != "") {
        $current_password = $doc->getElementsByTagName($current_user)->item(0)->getElementsByTagName('password')->item(0)->nodeValue;

        //$replace_password = $doc
    }
}

当仅使用为更改输入的用户名运行时,出现此错误

Catchable fatal error: Argument 1 passed to DOMNode::replaceChild() must be an instance of DOMNode, string given, called in E:\xampp\htdocs\CGS-Intranet\admin\html\useraction.php on line 252 and defined in E:\xampp\htdocs\CGS-Intranet\admin\html\useraction.php on line 201

有人可以向我解释如何执行此操作,或者向我展示他们将如何执行此操作..可能对我来说有点了解如何执行此操作:s

谢谢

$entered_new_username是一个字符串,因此您需要通过$doc->createElement()类的东西将其与DOM对象包装在一起

    $xml_old_user = $doc->getElementsByTagName('users')->item(0)->getElementsByTagName($current_username)->item(0)->replaceChild($doc->createElement($entered_new_username), $old_user);

这可能不太正确,但希望它能为您指明正确的方向。

好了,它编写并替换了我想要的节点,但是我遇到了其他必须解决的问题(即:它正在替换整个树,而不是仅仅更改节点名称)

无论如何,我使用的代码是

    // For initial debugging - to be deleted
if ($old_user == $current_username)
    echo    "old username found and matches";

// Check the variables to see if there is something to change in the data.
if ($entered_new_username != "") {
    try {
    $new_node_name = $doc->createElement($entered_new_username);

    $old_user->parentNode->replaceChild($new_node_name, $old_user);
    }
    catch (DOMException $e) {
        echo $e;
    }
    echo "Username is now: " . $current_username;
}

if ($new_pass != "") {
    $current_password = $doc->getElementsByTagName($current_user)->item(0)->getElementsByTagName('password')->item(0)->nodeValue;

    //$replace_password = $doc
}

$doc->save('../users/users.xml');

暂无
暂无

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

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