0

天,

作为我任务的一部分,我必须将用户在注册表单中输入的详细信息存储到 XML 文档中。

我已经设法做到了,但问题是当新用户注册时,旧的详细信息会被新用户的详细信息覆盖。因此,最终 XML 文档中只有 1 个用户详细信息。

我想知道是否有任何方法可以在“保留”旧详细信息的同时保存新用户的详细信息。

任何帮助表示赞赏:)

HTML 代码 -

<form id="rego" name="rego" method="get" action="register2.php">  
<table width="719" border="0">
<tr>
<td><div align="right">Email Address</div></td>
  <td><label for="email"></label>
  <input type="text" name="email" id="email" maxlength="50"  /></td>
</tr>
  <td width="376"><div align="right">First Name</div></td>
  <td width="333"><label for="firstName"></label>
  <input type="text" name="firstName" id="firstName" maxlength="15"  /></td>
  <td><div id="underInput"></div></td>
</tr>
<tr>
  <td><div align="right">Last Name</div></td>
  <td><label for="lastName"></label>
  <input type="text" name="lastName" id="lastName" maxlength="20"  /></td>
</tr>
<tr>
  <td><div align="right">Phone Number</div></td>
  <td><label for="phoneNumber"></label>
  <input type="text" name="phoneNumber" id="phoneNumber" maxlength="10"  /></td>
</tr>
<tr>

<tr>
  <td><div align="right">Password</div></td>
  <td><label for="password"></label>
  <input type="password" name="password" id="password"  maxlength="30"  /></td>
</tr>
<tr>
  <td><div align="right">Re-type password</div></td>
  <td><label for="confirmPassword"></label>
  <input type="password" name="confirmPassword" id="confirmPassword" maxlength="30" /></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td><input type="Submit" name="Submit" id="Submit" value="Submit"/><!--onclick="saveForm();"--></td>
</tr>

PHP 代码(我正在使用 DOM) -

<?php
    $CustomerEmail = $_GET["email"];
    $CustomerFName = $_GET["firstName"];
    $CustomerLName = $_GET["lastName"];
    $CustomerPhoneNumber = $_GET["phoneNumber"];
    $CustomerPassword = $_GET["password"];
    $CustomerConfirmPassword = $_GET["confirmPassword"];


    $doc = new DomDocument('1.0');

    $root = $doc->createElement('customers');
    $root = $doc->appendChild($root);

    $customer = $doc->createElement('customer');
    $customer = $root->appendChild($customer);

    $email = $doc->createElement('email');
    $email = $customer->appendChild($email);
    $valueEmail = $doc->createTextNode($CustomerEmail);
    $valueEmail = $email->appendChild($valueEmail);

    $fName = $doc->createElement('firstname');
    $fName = $customer->appendChild($fName);
    $valueFName = $doc->createTextNode($CustomerFName);
    $valueFName = $fName->appendChild($valueFName);

    $lName = $doc->createElement('lastname');
    $lName = $customer->appendChild($lName);
    $valueLName = $doc->createTextNode($CustomerLName);
    $valueLName = $lName->appendChild($valueLName);

    $phone = $doc->createElement('phone');
    $phone = $customer->appendChild($phone);
    $valuePhone = $doc->createTextNode($CustomerPhoneNumber);
    $valuePhone = $phone->appendChild($valuePhone);

    $password = $doc->createElement('password');
    $password = $customer->appendChild($password);
    $valuePassword = $doc->createTextNode($CustomerPassword);
    $valuePassword = $password->appendChild($valuePassword);

    $confirmPassword = $doc->createElement('confirmpassword');
    $confirmPassword = $customer->appendChild($confirmPassword); 
    $valueConfirmPassword = $doc->createTextNode($CustomerConfirmPassword);
    $valueConfirmPassword = $confirmPassword->appendChild($valueConfirmPassword);


    $doc->save('customer2.xml');
    ?>

对于任何不便,我深表歉意。

4

1 回答 1

0

您需要加载现有文件,然后每次创建一个新文件并覆盖以前的文件。

    .....
    $doc = new DomDocument('1.0');

    if($xml = file_get_contents( 'customer2.xml')){
      $doc->loadXML( $xml, LIBXML_NOBLANKS );
      $root=$doc->getElementsByTagName('customers')->item(0);
    }else{
      $root = $doc->createElement('customers');
      $root = $doc->appendChild($root);
    }

    $customer = $doc->createElement('customer');
    $customer = $root->appendChild($customer);
    .....
    $doc->save('customer2.xml');
于 2013-10-20T08:18:49.023 回答