由于某种原因,我在我的一个文本框中收到通知

时间:2011-03-26 12:25:14

标签: php

为什么我在referer文本框中获得以下文本:

  

注意:未定义的索引:referer in   第86行/var/www/register.php

何时加载以下脚本?

<?php
    error_reporting( E_ALL | E_STRICT );
    ini_set('display_errors', 1);
?>

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

        <?php
            $err = array();

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

                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 database1.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();

                        if( !empty( $_POST['referer'] ) ) {
                            $STH = $DBH -> prepare( "insert into database1.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;

                        header( "Location: ".$_SERVER['PHP_SELF'] );
                        exit;
                    } catch( PDOException $e ) {
                        error_log( $e -> getMessage() );

                        switch( $e -> getCode()) {
                            case 23000:
                                echo "Sorry, the referral ID you have entered does not exist.  However, your account has been created so you can add your referral details in later.";
                                break;
                            default:
                                echo $e -> getMessage();      
                        }
                    }


                } else {
                    foreach( $_POST as $key => $val ) {
                        $form[$key] = htmlspecialchars($val);
                    }
                }
            } else {
                $form['display_name'] = $form['email'] = $form['password'] = '';
            }
        ?>
    </head>

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

        <h1>register</h1>

        <form method="post">
            referers id:<br />
            <input type="text" name="referer" value="<?php echo $form['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>

2 个答案:

答案 0 :(得分:1)

当您第一次显示表单时,您正在执行此部分代码(第68行)

$form['display_name'] = $form['email'] = $form['password'] = '';

这会初始化$form数组的三个项目,这就是为什么当你尝试在第89,92和95行回显它们时,你不会收到通知。


referer的{​​{1}}项未初始化。

所以,第86行,当你试图回应它时:

$form

你收到通知。


两种可能的解决方案:

  • 不要尝试回显不存在的变量(或数组项)
    • isset()构造可以帮助测试。
  • 或在尝试使用之前初始化<input type="text" name="referer" value="<?php echo $form['referer']; ?>" /><br /><br />

答案 1 :(得分:1)

<?php echo $form['referer']; ?>

您正在尝试加载$ form ['referer']并且显然您的数组中没有'referer'条目。在第86行,将<?php echo $form['referer']; ?>更改为

<?php echo isset($form['referer'])?$form['referer']:''; ?>

-edited - 谢谢Pascal MARTIN的评论,这是真的