【问题标题】:foreach() error Warning: Invalid argument supplied for foreach()foreach() 错误警告:为 foreach() 提供的参数无效
【发布时间】:2012-03-10 14:30:54
【问题描述】:

对于我在方法中设置的一系列错误,我不断收到此错误

这里是代码

public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        foreach ($this->errors as $key => $value)
        {
            echo $value;
        }
    }

当我运行程序时,我不断收到此“警告:为 foreach() 提供的参数无效” 我像这样在构造函数中设置错误数组

 $this->errors = array();

所以我不完全确定为什么它不会打印错误!

public function validdata() {
        if (!isset($this->email)) {
            $this->errors[] = "email address is empty and is a required field";
        }

        if ($this->password1 !== $this->password2) {
            $this->errors[] = "passwords are not equal ";
        }
        if (!isset($this->password1) || !isset($this->password2)) {
            $this->errors[] = "password fields cannot be empty ";
        }
        if (!isset($this->firstname)) {
            $this->errors[] = "firstname field is empty and is a required field";
        }
        if (!isset($this->secondname)) {
            $this->errors[] = "second name field is empty and is a required field";
        }

        if (!isset($this->city)) {
            $this->errors[] = "city field is empty and is a required field";
        }


        return count($this->errors) ? 0 : 1;
    }

这是我如何将数据添加到数组本身!也感谢您的帮助!

好的,我在方法中添加了这个

public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        echo "<p>" . var_dump($this->errors) . "</p>";
        foreach ($this->errors as $key => $value)
        {
            echo $value;
        }

然后它在我的页面上输出这个

错误!! string(20) "提交无效!!"如果我在我的文本框中没有输入任何内容,所以它说它是一个字符串??

这也是我的构造函数,所以我是 php 新手!

  public function __construct() {

        $this->submit  = isset($_GET['submit'])? 1 : 0;
        $this->errors = array();
        $this->firstname = $this->filter($_GET['firstname']);
        $this->secondname = $this->filter($_GET['surname']);
        $this->email = $this->filter($_GET['email']);
        $this->password1 = $this->filter($_GET['password']);
        $this->password2 = $this->filter($_GET['renter']);
        $this->address1 = $this->filter($_GET['address1']);
        $this->address2 = $this->filter($_GET['address2']);

        $this->city = $this->filter($_GET['city']);
        $this->country = $this->filter($_GET['country']);
        $this->postcode = $this->filter($_GET['postcode']);


        $this->token = $_GET['token'];
    }

【问题讨论】:

  • 发布更多代码。我敢打赌,您尝试添加到 $this-&gt;errors 的某个地方,但不小心使用了 = 而不是 [] = 并最终用标量覆盖它...
  • 当我打字太快时,我有时会犯的一个常见错误是$this-&gt;errors = 'foo'; 而不是$this-&gt;errors[] = 'foo';。就这段代码而言,这不是问题。
  • 检查 var_dump 在 foreach 之前发生了什么。
  • $this-&gt;errors 最初可能是一个数组,但在调用showerrors 时肯定不是一个数组。那么为什么不直接将它的值转储到showerrors 看看它是什么?
  • @eoin 不行,去找设置字符串"invalid submission"的方法。这就是你的错误所在。

标签: php


【解决方案1】:

在对表单进行默认(未填写)验证时,设置了消息"invalid submission",您省略了括号[],导致$this-&gt;errors 被纯字符串覆盖,而不是附加到数组.

// Change
$this->errors = "invalid submission";

//...to...
$this->errors[] = "invalid submission";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多