I'm getting a notice in 1 of my textboxes for some reason
Why do I get the following text in the referer textbox:
Notice: Undefined index: referer in /var/www/register.php on line 86
when the following script loads?
开发者_Go百科<?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>
When you are first displaying your form, you are executing this portion of code (line 68) :
$form['display_name'] = $form['email'] = $form['password'] = '';
This initializes three items of your $form
array, which is why you don't get notices for those when you try to echo them on lines 89, 92, and 95.
But the referer
item of $form
is not initialized.
So, line 86, when you try to echo it :
<input type="text" name="referer" value="<?php echo $form['referer']; ?>" /><br /><br />
You get a notice.
Two possible solutions :
- Do not try to echo a variable (or item of an array) that doesn't exist.
- The
isset()
construct can help, to test for that.
- The
- Or initialize
$form['referer']
before trying to use it.
<?php echo $form['referer']; ?>
You're trying to load $form['referer'] and apparently you don't have a 'referer' entry in your array. On line 86, change <?php echo $form['referer']; ?>
to
<?php echo isset($form['referer'])?$form['referer']:''; ?>
-edited - Thank you Pascal MARTIN for your comment, which is true
精彩评论