这是我的代码:我想更新用户信息和风险因素。
public function getUserById($id)
{
$user = $this->_xml->xpath('//user[@id="' . $id . '"]');
return $user[0];
}
public function updateUser($post)
{
$user = $this->_xml->xpath('//user[@id="' . $post['id'] . '"]');
$user[0]->name= $post["name"];
$user[0]->date_of_birth= $post["date_of_birth"];
$user[0]->sex= $post["sex"];
$user[0]->age= $post["age"];
$this->_xml->asXML($this->path);
}
public function updateFactors($post)
{
$user = $this->_xml->xpath('//user[@id="' . $post['id'] . '"]');
//$user=new stdClass();
$user[0]->alcohol= $post["alcohol"]; // error here!
$user[0]->percentage= $post["percentage"];
$this->_xml->asXML($this->path);
}
if ($param == "edit") {
$id = $arr[1];
$user = $process->getUserById($id);
include 'myuser2.php';
if ($param == "update") {
$post = $_POST;
$process->updateUser($post);
include 'mylistar.php';
$process->listUsers();
}
if ($param == "editfactors") {
$id = $arr[1];
$user = $process->getUserById($id);
include'factors.php';
}
if($param == "updatefactors"){
$post = $_POST;
$process->updateFactors($post);
include 'mylistar.php';
$process->listUsers();
}
所以,“更新”,“编辑”,updateUser函数正在工作......但是因素部分不是!代码几乎是一样的,我不知道我做错了什么......对我来说似乎是逻辑。
错误:从空值创建默认对象(在行中 $ user [0] - > alcohol = $ post [“alcohol”]; )
我搜索并尝试了$user=new stdClass();
,但它不起作用:
Error: Cannot use object of type stdClass as array
你能帮忙吗? :/