我正在尝试根据表单提交更新XML元素文本。这是一个用户数据库,我使用用户的密码作为参考来更新他们的用户ID。由于所有密码都是唯一的,我认为这是一个很容易引用的元素。然而,每当我试图编辑一个UID时,操作都会失败,并转到我创建的错误页面。我不确定哪里出错了,任何帮助都将非常感激。
更新UID函数
function updateUID($pass, $file, $new)
{
$xml = new DOMDocument();
$xml->load($file);
$records = $xml->getElementsByTagName('UniqueLogin');
foreach ($records as $person) {
$passwordValue = $person->getElementsByTagName('Password')->item(0)->nodeValue;
// $personName = $person->getElementsByTagName('name')->item(0)->nodeValue;
if ($passwordValue == $pass) {
$idMatched = true;
$updatedText = $xml->createTextNode($new);
$personNode = $person->getElementsByTagName('UID')->item(0); // 需要定位到UID节点而不是替换整个person节点
$person->parentNode->replaceChild($updatedText, $personNode);
break;
}
}
if ($idMatched == true) {
if ($xml->save($file)) {
return true;
}
}
}
调用该函数的代码段
session_start();
include_once "includes/functions.inc.php";
include_once "includes/jdbh.inc.php";
include_once "includes/dbh.inc.php";
include_once "includes/ftpconn2.inc.php";
$file = $_SESSION['fileNameXML'];
if (file_exists($file)) {
if (isset($_POST['submit'])) {
$pass = $_POST['id'];
//$uid = $_SESSION['userid'];
$new = $_POST['uid'];
// 如果已设置提交且密码匹配,则尝试更新UID
if (updateUID($pass, $file, $new)) {
// 更新成功后执行FTP文件上传等操作
try {
$ftp_connection = ftp_connect($ftp_server);
if (!$ftp_connection) {
throw new Exception("无法连接到FTP服务器");
}
$loggedIn = ftp_login($ftp_connection, $ftp_user, $ftp_password);
if (!$loggedIn) {
throw new Exception('无法登录FTP');
}
$local_file1 = "HostSecurity.xml";
$remote_file1 = "HostSecurity.xml";
if (ftp_put($ftp_connection, $remote_file1, $local_file1, FTP_BINARY)) {
// 更新FTP上的文件成功
} else {
echo "上传文件时出现问题";
}
ftp_close($ftp_connection);
header("location: ../serverPasswords.php");
} catch (Exception $e) {
echo "失败:" . $e->getMessage();
}
header("location: ../serverPasswords.php");
} else {
header("location: ../serverPasswords.php?e=UIDNPD");
}
} else {
echo "缺少id";
}
} else {
echo "$file 文件不存在";
}
示例XML数据
<Unique_Logins>
<UniqueLogin>
<UID>AA23GHRDS657FGGRSF126</UID>
<Password>iMs0Az2Zqh</Password>
</UniqueLogin>
<UniqueLogin>
<UID>AA23GSDGFHJKDS483FGGRSF126</UID>
<Password>Ab7wz77kM</Password>
</UniqueLogin>
</Unique_Logins>