2014-11-22 23 views
2

所以我很忙这个系统来改变你的密码,当密码被改变时,一个绿色的方框出现,并显示'你的密码已被改变'。注意:使用Foreach时数组到字符串的转换

出现错误时,出现一个红色框,显示错误信息,例如: “您当前的密码不正确”或“您的新密码与验证密码不一致”。

所以,当有一个post请求,我count错误,如果有更多的则0,返回的错误是这样的:

public function nieuwpw($username, $currentpass, $newpass, $newpassconf) 
{ 
    $this->errors[] = array(); 
    $this->succes[] = array(); 
    $query = $this->db->conn->prepare('SELECT pass FROM ht_users WHERE naam = ?'); 
    $query->bind_param('s', $username); 
    $query->execute(); 
    $query->bind_result($dbpass); 
    $query->store_result(); 
    while ($query->fetch()) { 
     if (password_verify($currentpass, $dbpass)) 
     { 
      if ($newpass === $newpassconf) 
      { 
       $newpasshash = password_hash($newpass, PASSWORD_DEFAULT); 
       $stmt = $this->db->conn->prepare('UPDATE ht_users SET pass = ? WHERE naam = ?'); 
       $stmt->bind_param('ss', $newpasshash, $username); 
       $stmt->execute(); 
       $stmt->close(); 
      } 
       else 
      { 
       $this->errors[] = 'Whoops, je 2 nieuwe wachtwoorden zijn niet gelijk.'; 
      } 
     } 
      else 
     { 
      $this->errors[] = 'Whoops, je huidige wachtwoord klopt niet!'; 
     } 
    } 
    if (count($this->errors) > 0) 
    { 
     return $this->errors; 
    } 
    $this->succes[] = 'Je wachtwoord is met success gewijzigd!'; 
    return $this->succes; 
    $query->close(); 
} 

}

,这是我用它来修改密码的功能。以下是我如何“回响”的错误:

 <?php //24 
//25 
if(isset($users->succes))//26 
{//27 
    if (count($users->succes) > 0) //28 
    {//29 
     foreach ($users->succes as $succes) {//30 
        $content = '<div id="message_succes" style="background-color:#00B200; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $succes. '</p></div>';//31 
    echo $content;//32 
//33 
      } //34 
    }//35 
}//36 
if(isset($users->errors))//37 
{//38 
    if (count($users->errors) > 0) //39 
    {//40 
     foreach ($users->errors as $errors) {//41 
        $content = '<div id="message_succes" style="background-color:#ff0033; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $errors. '</p></div>';//42 
    echo $content;//43 
//44 
      } //45 
    }//46 
}//47 
//48 
?> //49 

而且还在,甚至尽管我只能随声附和错误或成功的消息,如果有更多的则1,我得到这些错误:

Notice: Array to string conversion in C:\xampp\htdocs\pages\page16.php on line 31 


Notice: Array to string conversion in C:\xampp\htdocs\pages\page16.php on line 42 

在第二个代码块中,我对行进行了编号,以便您可以看到哪些行会抛出错误。

当做var_dump,我得到下面的输出:

array(2) { [0]=> array(0) { } [1]=> string(41) "Whoops, je huidige wachtwoord klopt niet!" }

而且,我觉得很奇怪的是,它显示红色的错误框两次,连寿没有任何成功消息,它也显示了绿色框...

我希望这将被视为一个完整的问题,有人可以帮助我解决这个问题。谢谢。

回答

1

我认为错误的原因是您发布的第一个代码块的3线和4:

$this->errors[] = array(); 
$this->succes[] = array(); 

尝试改变这些线路分为:

$this->errors = array(); 
$this->succes = array(); 

在你的代码创建一个数组并立即将该数组的第一个元素设置为数组。你可以看到在你发布的var_dump上:

array(2){[0] => array(0){} [1] => string(41)“哎呀,je huidige wachtwoord klopt niet!” }

稍后,当您循环遍历$ errors或$ succes数组时,循环的第一次迭代将返回您设置为$ errors或$ succes数组的第一个元素的数组。然后你试着回应这个数组,php会告诉你正在回应一个数组。

我的建议修补程序是否正常工作,您是否看到代码为什么会中断?

PS:

  1. 尽量保持所有的代码在英语,所以它更容易对非讲荷兰语的人理解你的代码。
  2. 以下字符串是用荷兰语写的:'w wachtwoord遇到成功gewijzigd!'。在荷兰语中,'成功'是用'1'写成的,所以它应该是''w wachtwoord会遇到成功!'。

祝你好运!

1

你有没有试过像这样使用它?

$this->errors = array(); 
$this->succes = array(); 

,如果任何错误

foreach ($users as $user) { 
     if(isset($user->succes)) 
     { 
      $content = '<div id="message_succes" style="background-color:#00B200; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' .$user->succes. '</p></div>'; 
      echo $content; 

     } 
     if(isset($user->errors)) 
     { 
      $content = '<div id="message_succes" style="background-color:#ff0033; width:100%; height:30px;"><p style="color:#fff; font-family:Ubuntu; padding:5px;">' . $user->errors. '</p></div>'; 
      echo $content; 

     } 
    } 

评论...

相关问题