2014-10-18 1 views
1

html에서 php로 사용자 입력 데이터를 게시하려고 시도 할 때 오류가 발생하여 xml 파일에 저장되었습니다. 그것은 나에게이 오류를 준다. Catchable 치명적인 오류 : DOMNode :: appendChild()에 전달 된 인수 1은 DOMNode의 인스턴스 여야합니다. 여기에 코드dom cant 변수를 사용하여 XML에 데이터 저장

register.htm register.php의

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<title>test</title> 
</head> 
<body> 
<form id="regform" method="post" action="register.php"> 
<fieldset id="person"> 
<legend>Your details:</legend> 
<p><label for="fname">First Name:</label><input type="text" name="fname"/></p> 
<p><label for="lname">Last Name:</label><input type="text" name="lname"/></p> 
<p><label for="password">Password:</label><input type="password" name="password"/></p> 
<p><label for="cpassword">Confirm Password:</label><input type="password" name="cpassword"/></p>    
<p><label for="email">Email:</label><input type="email" id="email" name="email"/></p> 
<p><label for="phone">Phone:</label><input type="text" name="phone"/></p> 
<input type="submit" value="Register"/> 
</fieldset> 
</form> 
</body> 
</html> 

관련 부분을 (전체 코드가 너무 깁니다)

오류가 시사 하듯이
$fname = @trim($_POST["fname"]); 
$lname = @trim($_POST["lname"]); 
$password = @trim($_POST["password"]); 
$cpassword = @trim($_POST["cpassword"]); 
$phone = @trim($_POST["phone"]); 
$email = @trim($_POST["email"]); 
if(file_exists('../../customer.xml')) 
{ 
    $xml2 = file_get_contents('../../data/customer.xml'); 
} 

if(!file_exists('../../customer.xml')) 
     { 
      $dir = "../../data"; 
      $f = fopen("$dir/customer.xml", "w"); 
      $doc = new DOMDocument('1.0'); 
      $doc->formatOutput = true; 
      $root = $doc->createElement('customer'); 
      $doc->appendChild($root); 
      $user = $doc->createElement('user'); 
      $root->appendChild($user); 
      $fname = $doc->createElement('fname'); 
      @override - if I change this to 'brian' then it works, doesnt work with $variable 
      $fnameValue = $doc->createTextNode($fname); 
      $fname->appendChild($fnameValue); 
      $user->appendChild($fname); 
      $lname = $doc->createElement('lname'); 
      $lnameValue = $doc->createTextNode($lname); 
      $lname->appendChild($lnameValue); 
      $user->appendChild($lname); 
      echo $doc->save('../../data/customer.xml'); 
      //$doc->load('customer.xml'); 
      echo ' Registration Successful!'; 
     } 

답변

1

appendChild DOMNode을 필요로합니다. 해당 요소를 만든 다음 사용자 입력의 두 번째 매개 변수를 사용하면됩니다. 예 :

$fname = @trim($_POST["fname"]); 
$lname = @trim($_POST["lname"]); 
$password = @trim($_POST["password"]); 
$cpassword = @trim($_POST["cpassword"]); 
$phone = @trim($_POST["phone"]); 
$email = @trim($_POST["email"]); 

if(file_exists('../../customer.xml')) { 
    $xml2 = file_get_contents('../../data/customer.xml'); 
} 

if(!file_exists('../../customer.xml')) { 
    $dir = "../../data"; 
    $f = fopen("$dir/customer.xml", "w"); 
    $doc = new DOMDocument('1.0'); 
    $doc->formatOutput = true; 

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

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

    $fname_node = $doc->createElement('fname', $fname); 
    $user->appendChild($fname_node); 

    $lname_node = $doc->createElement('lname', $lname); 
    $user->appendChild($lname_node); 

    echo $doc->save('../../data/customer.xml'); 
    //$doc->load('customer.xml'); 
    echo ' Registration Successful!'; 
} 
1

$fname 변수를 다시 사용하고 있습니다. 그래서 당신은 오류가 발생합니다. 그것은 단순한 인쇄상의 실수입니다. 입력 변수 앞에 input이라는 접두어를 붙이는 것입니다. $input_fname 그래서 당신이 거기에서 얻는 것이 확실합니다. 또는 심지어 $input = new stdClass;과 같은 stdclass를 만든 다음 변수를 할당하면됩니다. 즉, 그것이 의미하는 바를 분명히하고 쉽게 더 쉽게 입력을 전달할 수 있습니다.

예 :

$input = new stdClass; 

$input->fname  = @trim($_POST["fname"]); 
$input->lname  = @trim($_POST["lname"]); 
$input->password = @trim($_POST["password"]); 
$input->cpassword = @trim($_POST["cpassword"]); 
$input->phone  = @trim($_POST["phone"]); 
$input->email  = @trim($_POST["email"]); 

$doc = new DOMDocument('1.0'); 
$doc->formatOutput = true; 

$root = $doc->createElement('customer'); 
$root = $doc->appendChild($root); 
foreach ($input as $key => $value) { 
    $element = $doc->createElement($key, $value); 
    $root->appendChild($element); 
} 

$doc->save('php://output'); 

예시 출력 :

<?xml version="1.0"?> 
<customer> 
    <fname>Waltraud</fname> 
    <lname>Van H&#xC3;&#xB6;mpenstetten</lname> 
    <password></password> 
    <cpassword></cpassword> 
    <phone></phone> 
    <email></email> 
</customer> 

온라인 데모 : https://eval.in/private/29c08d9fafec22

관련 문제