Warning in foreach loop?
Why am I getting the following warning:
开发者_JAVA百科[Fri Mar 25 19:19:21 2011] [error] [client 127.0.0.1] PHP Warning: Invalid argument supplied for foreach() in /var/www/register.php on line 60, referer: http://localhost/register.php
when running the following script:
<?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>
You are getting the error because you are trying to access a variable that, under certain conditions, doesn't exist. If your page's form wasn't submitted, the $err
variable wouldn't exist.
You can fix this by placing $err = array();
outside your if
statement. Change:
if( $_SERVER['REQUEST_METHOD']=='POST' ) {
$err = array();
To:
$err = array();
if( $_SERVER['REQUEST_METHOD']=='POST' ) {
That error occurs when the argument is not an array. You should put if(is_array($_POST))
before/around your foreach
to make sure it's not supplied with a null value.
I think your session isn't always initialized so you get an invalid argument because it doesn't exist
This is the kind of warning you get when you try to iterate over something that is not an array.
Before the following line :
foreach( $err as $line ) {
Make sure that $err
is indeed an array.
Two solutions :
- initialize
$err
earlier, outside of the condition that tests if$_SERVER['REQUEST_METHOD']=='POST'
, so it at least contains an empty array - Or don't try to iterate over
$err
if it's not set -- usingisset()
to test for that
精彩评论