foreach循环中的警告?

时间:2011-03-25 20:47:14

标签: php

为什么我收到以下警告:

  

[Fri Mar 25 19:19:21 2011] [错误]   [client 127.0.0.1] PHP警告:   提供的参数无效   foreach()在/var/www/register.php上   第60行,引用者:   http://localhost/register.php

运行以下脚本时:

<?php
    error_reporting( E_ALL );
?>

<html>
    <head>
        <title></title>
        <link rel="icon" type="image/png" href="favicon.ico">

        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' ) {
                $err = array();

                if( empty( $_POST['display_name'] ) ) $err[] = "display name field is required";
                if( empty( $_POST['email'] ) ) $err[] = "email field is required";
                if( empty( $_POST['password'] ) ) $err[] = "password field is required";

                if( !$err ) {
                    try {
                        $DBH = new PDO( "mysql:host=localhost;dbname=database1", "user", "pass" );
                        $DBH -> setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

                        $STH = $DBH -> prepare( "insert into table1 (display_name, email, password) values ( :display_name, :email, :password )" );

                        $STH -> bindParam( ':display_name', $_POST['display_name'], PDO::PARAM_STR, 100 );
                        $STH -> bindParam( ':email', $_POST['email'], PDO::PARAM_STR, 100 );
                        $STH -> bindParam( ':password', $_POST['password'], PDO::PARAM_STR, 100 );

                        $STH -> execute();

                        $STH = $DBH -> prepare( "insert into table2 ( username, status, users_id ) values ( :username, :status, :users_id )" );

                        $strStatus = 1;

                        $STH -> bindParam( ':username', $_POST['display_name'], PDO::PARAM_STR, 100 );
                        $STH -> bindParam( ':status', $strStatus, PDO::PARAM_INT, 1 );
                        $STH -> bindParam( ':users_id', $_POST['referer'], PDO::PARAM_INT, 1 );

                        $STH -> execute();

                        $DBH = null;
                    } catch( PDOException $e ) {
                        echo $e -> getMessage();
                    }

                    header( "Location: ".$_SERVER['PHP_SELF'] );
                    exit;
                } else {
                    foreach( $_POST as $key => $val ) {
                        $form[$key] = htmlspecialchars($val);
                    }
                }
            } else {
                $form['display_name'] = $form['email'] = $form['password'] = '';
            }
        ?>
    </head>

    <body>
        <?php foreach( $err as $line ) { ?>
        <div style="error"><?php echo $line; ?></div>
        <?php } ?>

        <h1>register</h1>

        <form method="post">
            referers id:<br />
            <input type="text" name="referer" /><br /><br />

            name:<br />
            <input type="text" name="display_name" value="<?php echo $form['display_name']; ?>" /><br /><br />

            email:<br />
            <input type="text" name="email" value="<?php echo $form['email']; ?>" /><br /><br />

            password:<br />
            <input type="text" name="password" value="<?php echo $form['password']; ?>" /><br /><br />

            <input type="submit" value="register" />
        </form>
    </body>
</html>

4 个答案:

答案 0 :(得分:2)

您收到错误是因为您尝试访问在某些条件下不存在的变量。如果您的网页表单未提交,则$err变量将不存在。

您可以将$err = array();放在if声明之外来解决此问题。变化:

if( $_SERVER['REQUEST_METHOD']=='POST' ) {
   $err = array();

要:

$err = array();
if( $_SERVER['REQUEST_METHOD']=='POST' ) {

答案 1 :(得分:0)

当参数不是数组时会发生该错误。您应该在if(is_array($_POST))之前/周围放置foreach,以确保它没有提供空值。

答案 2 :(得分:0)

我认为你的会话并不总是被初始化,所以你得到一个无效的参数,因为它不存在

答案 3 :(得分:0)

当您尝试迭代不是数组的内容时,会发出这种警告。

在以下行之前:

foreach( $err as $line ) {

确保$err确实是一个数组。


两种解决方案:

  • 之前初始化$err ,超出了测试$_SERVER['REQUEST_METHOD']=='POST'的条件,因此它至少包含一个空数组
  • 如果没有设置,请不要尝试迭代$err - 使用isset()来测试