The following script is generating a warning because the $err is inside the if statement. Why are the errors not displaying on the actual PHP page?
<?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("delete 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("delete 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>
Can someone please help me with this?