【问题标题】:foreach invalid argument and undefined variableforeach 无效参数和未定义变量
【发布时间】:2011-04-28 20:55:43
【问题描述】:

我正在尝试创建一个表单,但在这些行中出现错误。

 else 
 {
  //report the errors.

 echo '<h1> Err... </h1>
 <p> The following error(s) have occured</p>';

 foreach ($errors as $msg)
     {
   echo "--$msg<br />\n";
  }
  echo '</p><p>Please Try Again.</p><p><br/></p>';

 }

那么,怎么了??这是错误消息 -

错误...

出现以下错误 -

注意:未定义的变量:错误 C:\wamp\www\password.php 在第 107 行

警告:提供的参数无效 C:\wamp\www\password.php 中的 foreach() 在第 107 行,请重试。

我已将错误设置为数组。

我上面的代码--

if(isset($_POST['提交'])) {

require_once('C:\wamp\www\connect.php');
//connecting to db

$errors = array();

if (empty($_POST['email']))

{
    $errors[]='Please enter a valid email address.';
}

这是我的完整代码 -

//forgot password update

include('C:\wamp\www\header.html');

//check if form has been submitted
require_once('C:\wamp\www\connect.php');
    //connecting to db



if(isset($_POST['submitted'])) {
    $errors = array();


    if (empty($_POST['email']))

    {
        $errors[]='Please enter a valid email address.';
    }

    else
    {
      $e = mysqli_real_escape_string($db_name,trim($_POST['email']));
    }

    //check for current password
    if (empty($_POST['password']))

    {
        $errors[]='Current password does not match.';
    }

    else
    {
      $p = mysqli_real_escape_string($db_name,trim($_POST['password']));
    }

    //check for a new password and match with confirm pass.

    if(!empty($_POST['password1']))
    {
        if($_POST['password1'] != $_POST['cpass'])
        {
            $errors[] = 'The entered password and confirm password do not match.';
        }
        else
        {
            $np=mysqli_real_escape_string($db_name,trim($_POST['password1']));
        }
    }
    if(empty($errors))
    //if everything is fine.

    //verify the entered email address and password.

    $q="SELECT username FROM users WHERE (email='$e' AND password=SHA1('$p'))";
    $r=@mysqli_query($db_name,$q);
    $num = @mysqli_num_rows($r);
    if($num==1)
    //if it matches.

    //get user id
    {
    $row=mysqli_fetch_array($r, MYSQLI_NUM);

    //udpdate query.

    $q="UPDATE users SET password= SHA1('$np') WHERE username=$row[0]";

    $r=@mysqli_query($db_name, $q);

    if (mysqli_affected_rows($db_name) ==1)

    {
        echo '<h3>Your password has been updated.</h3>';
    }

    else {
        echo '<h3>Whops! Your password cannot be changed due a system error. Try again later. Sorry</h3>';

    echo '<p>' .mysqli_error($db_name). 'Query:' . $q.'</p>';
    }


    exit();
    }
    else 
    {

        //invalid email and password

        echo 'The email address and password do not match';
    }

}
    else 
    {
        //report the errors.

    echo '<h1> Err... </h1>
    <p> The following error(s) have occured</p>';

    foreach ($errors as $msg)
        {
            echo "--$msg<br />\n";
        }
        echo '</p><p>Please Try Again.</p><p><br/></p>';

    }


    ?>

【问题讨论】:

  • 问题是,不说清楚,变量 $errors 没有定义,因此不是foreach 可以迭代的数组。如果您可以在此之前提供有关您的代码的更多详细信息,我们可能会提供帮助。
  • 它基本上说:Undefined variable: errors,这意味着你有一个未定义的变量$errors
  • 那么,if 之后是 else 吗?
  • 我已经添加了我的完整代码。
  • 您编辑问题以删除内容有什么原因吗?

标签: php


【解决方案1】:

你有两个问题。第一个是空/不存在数组的原因,第二个是缺乏对它的测试。

首先是您在 if 块内测试错误,然后在 else 块内循环它们。

if (isset($_POST['submitted'])) {
  // create errors array and set errors
} else {
  // loop through array of errors
}

因此,如果设置了错误,则脚本不会进入循环。如果脚本进入循环,则没有设置错误。

第二个是你应该在测试完数组之后才进入foreach循环:

if (!empty($errors) && is_array($errors)) { // use this line and get rid of the else.
  foreach ($errors as $msg) {
        echo "--$msg<br />\n";
  }
  echo '</p><p>Please Try Again.</p><p><br/></p>';
 } // and close it.

【讨论】:

    【解决方案2】:

    将“$errors = array()”的声明移到“if(isset($_POST['submitted'])) { " 一切都应该正常!

    【讨论】:

      【解决方案3】:

      您的foreach 循环超出了$error 数组的定义范围。

      简而言之您的代码:

      if(isset($_POST['submitted'])) {
          $errors = array();
      } else {
          foreach($errors as $error)
      }
      

      如果$_POST 未设置,则您的$errors 未定义。

      【讨论】:

        【解决方案4】:

        基本上,这里发生的是您在定义 $errors 之前使用它。

        您可能需要在脚本顶部附近设置“$errors = array()”,以便它始终至少是一个空数组。

        【讨论】:

          【解决方案5】:

          没有名为$errors 的数组。您将不得不进一步查看您的脚本,为什么不这样做。

          您可以通过使用来修复错误消息

          if (!empty($errors) and (is_array($errors)))
           foreach ($errors as $msg)
          

          【讨论】:

          • 我设置了错误......这是我上面代码的预览...... if(isset($_POST['submitted'])) { require_once('C:\wamp\www\连接.php'); //连接到数据库 $errors = array(); if (empty($_POST['email'])) { $errors[]='请输入有效的电子邮件地址。'; }
          • @Johnson 很奇怪。您可以在单独的代码块中按原样发布完整的代码吗?
          • @Johnson 你在错误的地方执行$errors = array();,在条件检查isset($_POST['submitted']) 内。您需要将其带到主脚本中
          • 如果我这样做,Err 的 else 部分仍然会显示,但仍会显示出现以下错误,但错误已消失。
          • @Johnson 这是因为您在未设置 $_POST["submitted"] 时显示该块。听起来这不是你想做的事
          猜你喜欢
          • 2023-03-27
          • 2016-04-14
          • 2020-04-19
          • 2012-05-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-02-28
          相关资源
          最近更新 更多