G'day,
В качестве части моего задания я должен хранить данные, введенные пользователем в регистрационной форме, в XML-документ.
Мне удалось это сделать, но проблема в том, что когда регистрируется новый пользователь, старые детали переписываются с деталями нового пользователя. Таким образом, в конце документа есть только 1 пользовательская информация в документе XML.
Мне было интересно, есть ли способ сохранить детали нового пользователя, "сохраняя" старые детали.
Любая помощь приветствуется :)
Код 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> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td><input type="Submit" name="Submit" id="Submit" value="Submit"/><!--onclick="saveForm();"--></td>
</tr>
PHP Code (я использую 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');
?>
Прошу прощения за любые неудобства.
Вам необходимо загрузить существующий файл, прежде чем создавать новый каждый раз и переопределять предыдущий.
.....
$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');