I am trying to edit the information for a user and then save that new information to the xml file to display on another page but the updates are not being saved. Here is the php code to edit a user:
<?php
require 'simplexml.class.php';
$users=simplexml_load_file('UserList.xml');
if (isset($_POST['savebutton']))
{ foreach ($users->user as $user){
if($user['firstName']==$_POST['firstName']){
$user->firstName= $_POST['firstName'];
$user->lastName= $_POST['lastName'];
$user->email= $_POST['email'];
$user->password= $_POST['password'];
$user->address= $_POST['address'];
$user->number= $_POST['number'];
break;
}
}
file_put_contents('UserList.xml', $users->asXML('UserList.xml'));
header('location: P9.php');
}
foreach ($users->user as $user){
if($user['firstName']==$_GET['firstName']){
$firstName= $user->firstName;
$lastName= $user->lastName;
$email= $user->email;
$password= $user->password;
$address= $user->address;
$number= $user->number;
break;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<link rel="stylesheet" href="style2.css">
<link rel="stylesheet" href="Main-Stylesheet.css">
</head>
<body>
<!--header-->
<nav >
<a href="index.html">
<img src="Images/Logo.png">
</a>
<form >
<input type="search" placeholder="'Product'">
<button type="submit">Search</button>
</form>
<a href="ShoppingCart.html">
<div >
<img src="Images/cart-logo.png" />
</div>
</a>
</nav>
<!--Content-->
<div >
<div >
<nav id="sidebarMenu" >
<div >
<ul >
<li >
<a aria-current="page" href="P7.html">
Products
</a>
</li>
<li >
<a href="P11.html">
Orders
</a>
</li>
<li >
<a href="P9.html">
User List
</a>
</li>
</ul>
</div>
</nav>
<form method="POST" action="P9.php">
<main >
<div >
<h1 >Edit User</h1>
<div >
<button type="submit" name="savebutton">
Save
</button>
</div>
</div>
<div >
<div >
<input type="text" placeholder="First name" aria-label="First name" name="firstName" value="<?php echo $firstName; ?>">
</div>
<div >
<input type="text" placeholder="Last name" aria-label="Last name" name="lastName" value="<?php echo $lastName; ?>">
</div>
</div>
<br>
<div >
<label for="exampleFormControlInput1" >Email address</label>
<input type="email" id="exampleFormControlInput1" placeholder="[email protected]" name="email" value="<?php echo $email; ?>">
</div>
<div >
<label for="inputPassword" >Password</label>
<div >
<input type="password" id="inputPassword" name="password" value="<?php echo $password; ?>">
</div>
</div>
<br>
<div >
<label for="inputAddress" >Address</label>
<input type="text" id="inputAddress" name="address" placeholder="1234 Main St" value="<?php echo $address; ?>">
</div>
<br>
<div >
<label for="inputAddress" >Phone Number</label>
<input type="text" id="inputAddress" name="number" placeholder="i.e. 123456" value="<?php echo $number; ?>">
</div>
</form>
</main>
<!-- footer section -->
<div >
<div >
<h2>About Us</h2>
<a href="#">Our Story</a>
<a href="#">Blog</a>
<a href="P7.html">Customers</a>
</div>
<div >
<h2>Customer Service</h2>
<a href="#">Contact Us</a>
<a href="#">Terms and Conditions</a>
<a href="#">Find a Store</a>
<a href="#">FAQ</a>
</div>
<div >
<h2>Social Media</h2>
<a href="#">Instagram</a>
<a href="#">Facebook</a>
<a href="#">YouTube</a>
<a href="#">Twitter</a>
</div>
<div >
<div >
© 2022 poeatry.com
</div>
<div >
</div>
</div>
</div>
<!-- footer section -->
</body>
</html>
Here is the xml file:
<?xml version="1.0"?>
<UserList>
<info>
<FirstName> </FirstName>
<LastName> </LastName>
<Email> </Email>
<Password> </Password>
<Address> </Address>
<Number> </Number>
</info>
</UserList>
Currently when i click on the edit button from my display page it shows me the correct item but the new information updated is never saved to the xml.
CodePudding user response:
Currently, posted XML does not have a <user>
node which you iterate on but <info>
node. Simply adjust your path to actual tag. Additionally, XML tag names are case sensitive (i.e., firstName != FirstName
). Even better, name PHP variables in line with XML nodes.
Also, consider escaping user input for special XML entities with htmlspecialchars
to ensure legal XML.
require 'simplexml.class.php';
$userList = simplexml_load_file('UserList.xml');
if (isset($_POST['savebutton'])) {
foreach ($userList->info as $info) {
if((string)$info->FirstName == $_POST['firstName']) {
$info->FirstName = htmlspecialchars($_POST['firstName']);
$info->LastName = htmlspecialchars($_POST['lastName']);
$info->Email = htmlspecialchars($_POST['email']);
$info->Password = htmlspecialchars($_POST['password']);
$info->Address = htmlspecialchars($_POST['address']);
$info->Number = htmlspecialchars($_POST['number']);
break;
}
}
$userList->asXML('UserList.xml');
header('location: P9.php');
}
CodePudding user response:
The PHP code for recording data refers to UserList
's child node as user
, but your XML code calls it info
.
Since the SimpleXMLElement Object does not have a child node called user
, it returns an empty object. SimpleXML doesn't trigger an exception for calling a node that doesn't exist so it wouldn't show up in the error log. The code would just proceed and nothing would get saved.
This should work:
foreach ($users->info as $user){
if($user['firstName']==$_POST['firstName']){
$user->firstName= $_POST['firstName'];
$user->lastName= $_POST['lastName'];
$user->email= $_POST['email'];
$user->password= $_POST['password'];
$user->address= $_POST['address'];
$user->number= $_POST['number'];
break;
}
}
or you could change the XML document to:
<?xml version="1.0"?>
<UserList>
<user>
<FirstName> </FirstName>
<LastName> </LastName>
<Email> </Email>
<Password> </Password>
<Address> </Address>
<Number> </Number>
</user>
</UserList>