2015-10-15 175 views
0

当我尝试使用我的某个函数时,出现invalid argument supplied for foreach()错误。为foreach提供的无效参数()

我看了一些其他的例子,但找不到任何匹配的情况。

我的代码如下。

user.php的:

public function activateAccount($email = null, $email_code = null) { 

     $field = 'email'; 
     $data = $this->_db->get('users', array($field, '=', $email)); 

     if($this->_db->count()) { 
      $this->_data = $this->_db->first(); 
     } 

     if($this->emailExists($email)) { 
      if($this->data()->verified == 0) { 
       $this->updateActivation('users', $email, array(
        'verified' => 1 
       )); 
       return true; 
      } else { 
       return false; 
      } 
     } 
     return false; 

    } 

public function updateActivation($table, $fields = array(), $email = null) { 

     if(!$this->_db->updateActivation($table, $email, $fields)) { 
      throw new Exception('An unexpected error occurred'); 
     } 
    } 

db.php中:

public function updateActivation($table, $email, $fields) { 
     $set = ''; 
     $x = 1; 

     foreach($fields as $name => $value) { 
      $set .= "{$name} = ?"; 

      if($x < count($fields)) { 
       $set .= ', '; 
      } 
      $x++; 
     } 

     $sql = "UPDATE {$table} SET {$set} WHERE email = {$email}"; 
     if(!$this->query($sql, $fields)->error()) { 
      return true; 
     } 

     return false; 
    } 

如果需要更多的代码,请让我知道。

任何帮助,非常感谢。

谢谢!

回答

0

你有错误的顺序在参数表:

$this->updateActivation('users', $email, array('verified' => 1)); 

电话

updateActivation($table, $fields = array(), $email = null) { 

我想这应该是

$this->updateActivation('users', array('verified' => 1), $email); 

+0

是的,就是这样。谢谢! – Chris

0

public function updateActivation($table, $fields = array(), $email = null) { 

User.php具有分配不正确的变量。应该

public function updateActivation($table, $email = null, $fields = array()) { 

或执行时的功能颠倒顺序相匹配(我想给你的其他函数调用和赋值前者是更好的方法)。

$this->updateActivation('users', array(
        'verified' => 1 
       ), $email); 
+0

这确实是错误。谢谢! – Chris

相关问题