爲什麼會收到以下警告:foreach循環中的警告?
[週五3月25日19時19分21秒2011] [錯誤] [客戶端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>