嘿所以我目前有一张表格,我想要的是我想要的除了一件事,我试图确保输入的电子邮件是有效的电子邮件地址我被告知我可以使用FILTER_VALID_EMAIL但是如果我这样做&#39 ;我将得到相同的错误消息,说你已经订阅了。
这里是我可以在什么时候解决我的问题?会有太多的贬义! :)
<?php
// $_POST might be empty even if the request is a POST
if ('POST' === $_SERVER['REQUEST_METHOD']) {
try {
$db = new PDO('mysql:host=localhost;dbname=test', $subsc_username, $subsc_password);
// default error mode is to return FALSE
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die($e->getMessage());
}
$subsc_email = $_POST['sub_email'];
// Insert form valudes in to query and process sql query
$sql = 'INSERT INTO subscribers (subsc_email) VALUES (:subsc_email)';
try {
if ( ! filter_var( $subsc_email , FILTER_VALID_EMAIL )) throw new Exception('Invalid Email');
$query = $db->prepare($sql);
$query->execute(array(
':subsc_email' => $subsc_email,
));
echo '<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Success!</strong> You have been added to our mailing list.</div>'; // Success Message
} catch( PDOException $e ) {
echo '<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
<strong>Error!</strong> You are already subsribed to our mailing list...</div>'; // Error Message
}
}
?>
<form method="post" action="">
<div class="form-group">
<label>Email:</label>
<input class="form-control" type="text" name="sub_email" />
<br />
<div class="pull-right">
<input class="btn btn-success" type="submit" value="Subscribe" />
</div>
</div>
</form>
答案 0 :(得分:1)
存在语法错误。 (缺少字母ATE
以完成应该读作VALIDATE
)
您需要使用: filter_var( $subsc_email , FILTER_VALIDATE_EMAIL )
而不是你现在拥有的东西:
filter_var( $subsc_email , FILTER_VALID_EMAIL )
---Missing -^ (ATE)
替换为以下内容:
( ! filter_var( $subsc_email , FILTER_VALIDATE_EMAIL ))