Editing XML Files with PHP

HI i want to update details from a form and save it into current xml file but having problem
ok so far i can search and get the correct info to the form.
xml code:
<customer id="BL123456">
            <name>
                  <title/>
                  <firstName>Mary</firstName>
                  <middleName/>
                  <lastName>Smith</lastName>
            </name>
</customer>
just testing out the first name only but not working count brins in the correct value of the item.
if (array_key_exists('edit', $_POST)) { 
				$file = "files/customers.xml"; 
				$doc = new DOMDocument();
			    $doc->load( $file);
				
	$fName = $_POST['fname'];
	$lName = $_POST['lname'];
	$count = $_POST['count'];
	if (!$fName == "" || !$lName == ""){
	$ufnames = $doc->getElementsByTagName( 'firstName' );
	$ufname = $ufnames->item($count)->nodeValue = $fName;
	$doc->saveXML();
	}
}

// ...
// ... form
<tr><td align="right">First Name:</td><td><input type="text" name="fname" value="'.$fname.'" size="30" /></td></tr>
			<tr><td align="right">Middle Name:</td><td><input type="text" name="mname" value="'.$mname.'" size="30"/></td></tr>
			<tr><td align="right">Last Name:</td><td><input type="text" name="lname" value="'.$lname.'" size="30"/></td></tr>
<input type="submit" value="Update" name="update" />

Open in new window

LVL 4
bobby101Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rjdownCommented:
Where you have your input elements like this:

<input type="text" name="fname" value="'.$fname.'" size="30" />

You need to use:

<input type="text" name="fname" value="<?php echo $fname; ?>" size="30" />

In fact, even better would be using htmlspecialchars to ensure any data with special characters in does not break your page:

<input type="text" name="fname" value="<?php echo htmlspecialchars($fname);  ?>" size="30" />

Also, consider using the XML parser that comes with PHP5, SimpleXML: http://php.net/manual/en/book.simplexml.php
0
bobby101Author Commented:
no srry didnt wrk
0
rjdownCommented:
Ahh. Variable names care case-sensitive. Try

<input type="text" name="fname" value="<?php echo htmlspecialchars($fName);  ?>" size="30" />
0
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

bobby101Author Commented:
no that not it Ill acctach the file so you can see what im tryin to do.
its getting the customer from xml into the form but when i update it doesnt save it into the xml file

the xml code is
<customers>
<customer id="BL123456">
            <meter>987654321</meter>
            <name>
                  <title/>
                  <firstName>Mary</firstName>
                  <middleName/>
                  <lastName>Smith</lastName>
            </name>
            <address>
                  <street>23 Street St</street>
                  <city>Campbelltown</city>
                  <postcode>2566</postcode>
                  <state>NSW</state>
            </address>
            <phone>
                  <home>0298576433</home>
                  <mobile>0413335678</mobile>
                  <work>0296784455</work>
            </phone>
</customer>
</customers>
<?PHP 
session_start();
if (!$_SESSION['username']){
	header('Location: login.php');
}
$error = ""; 
if (array_key_exists('search', $_POST)) {   

		$by = $_POST['type'];
        $target = $_POST['target']; 
		
	if(!$target == "" && !$by == ""){
		
        $count = 0;
        $file = "files/customers.xml"; 
		  
			  $doc = new DOMDocument();
			  $doc->load( $file);
			  
			  $node = $doc->getElementsByTagName($by);
			  foreach( $node as $find ){
			   $customers = $find->nodeValue;
				  if ($target == $customers){
						$fnames = $doc->getElementsByTagName( 'firstName' );
						$fname = $fnames->item($count)->nodeValue;
						$lnames = $doc->getElementsByTagName( 'lastName' );
						$lname = $lnames->item($count)->nodeValue;
						$mnames = $doc->getElementsByTagName( 'middleName' );
						$mname = $mnames->item($count)->nodeValue;
						
						$streetE = $doc->getElementsByTagName( 'street' );
						$street = $streetE->item($count)->nodeValue;
						$cityE = $doc->getElementsByTagName( 'city' );
						$city = $cityE->item($count)->nodeValue;
						$pcode = $doc->getElementsByTagName( 'postcode' );
						$pc = $pcode->item($count)->nodeValue;
						$stateE = $doc->getElementsByTagName( 'state' );
						$state = $stateE->item($count)->nodeValue;
						
						$homeE = $doc->getElementsByTagName( 'home' );
						$hph = $homeE->item($count)->nodeValue;
						$mobileE = $doc->getElementsByTagName( 'mobile' );
						$mph = $mobileE->item($count)->nodeValue;
						$workE = $doc->getElementsByTagName( 'work' );
						$wph = $workE->item($count)->nodeValue;

						$c = $count;
					}
			   $count++;
			  }
			 			  
	} 
	 else{$error = "Unsuccessful";}
}



//update customer
if (array_key_exists('edit', $_POST)) { 
				$file = "files/customers.xml"; 
				$doc = new DOMDocument();
			    $doc->load( $file);
				
	$fName = $_POST['fname'];
	$lName = $_POST['lname'];
	$count = $_POST['count'];
	if (!$fName == "" || !$lName == ""){
	$ufnames = $doc->getElementsByTagName( 'firstName' );
	$ufname = $ufnames->item($count)->nodeValue = $fName;
	$doc->saveXML();
	//echo $count;
	}
}
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Energetic Energy Management</title>
</head>
<body>
	<h3>Welcome, <?php echo $_SESSION['username']; ?></h3>
	<a href="logout.php">Logout</a>
	<p>&raquo; <a href="index.php">Home</a>

<form name="search" method="post" action=""> 
<h3>Update Customer</h3>
<p><span style="color:red;"><?php echo $error ?></span></p>
<p>Search Type: <input type="radio" name="type" value="meter" />Meter Number <input type="radio" name="type" value="lastName" /> Surname
<p>Search: <input type="text" name="target" size="20" /></p>
<input type="submit" value="Go" name="search" />
<input type="hidden" name="search" value="1"/>
</form></p> 
<hr>
<form name="edit" method="post" action="">
		<table cellspacing="3">
			<tr><td align="right">First Name:</td><td><input type="text" name="fname" value="<?php echo htmlspecialchars($fname); ?>" size="30" /></td></tr>
			<tr><td align="right">Middle Name:</td><td><input type="text" name="mname" value="<?php echo htmlspecialchars($mname); ?>" size="30"/></td></tr>
			<tr><td align="right">Last Name:</td><td><input type="text" name="lname" value="<?php echo htmlspecialchars($lname); ?>" size="30"/></td></tr>
			<tr><td align="right">Street:</td><td><input type="text" name="street" value="<?php echo htmlspecialchars($street); ?>" size="30"/></td></tr>
			<tr><td align="right">City:</td><td><input type="text" name="city" value="<?php echo htmlspecialchars($city); ?>" size="30"/></td></tr>
			<tr><td align="right">Postcode:</td><td><input type="text" name="pc" value="<?php echo htmlspecialchars($pc); ?>" size="10"/></td></tr>
			<tr><td align="right">State:</td><td><input type="text" name="state" value="<?php echo htmlspecialchars($state); ?>" size="10"/></td></tr>
			<tr><td align="right">Home Phone:</td><td><input type="text" name="hph" value="<?php echo htmlspecialchars($hph); ?>" size="30"/></td></tr>
			<tr><td align="right">Mobile Number:</td><td><input type="text" name="mph" value="<?php echo htmlspecialchars($mph); ?>" size="30"/></td></tr>
			<tr><td align="right">Work Phone:</td><td><input type="text" name="wph" value="<?php echo htmlspecialchars($wph); ?>" size="30"/></td></tr>
			<tr><td align="right"></td><td><input type="submit" value="Update" name="update" /><input type="hidden" name="edit" value="1"/>
			<input type="hidden" name="count" value="<?php echo htmlspecialchars($c); ?>"/>
			</td></tr>
		</table>	  
	  </form>	

</form> 
</body>

Open in new window

0
bobby101Author Commented:
ok i got it was the DOM saveXML function
$doc->save('files/customers.xml'); (correct)
NOT
$doc->saveXML();  (incorrect)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Avinash ZalaWeb ExpertCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Components

From novice to tech pro — start learning today.