dongzhang4301 2014-01-30 00:10
浏览 29
已采纳

试图抛出自定义异常以验证电子邮件

Hey so I currently have a form working pretty much how I want except one thing I am trying to make sure the emails that are being entered are valid email addresses I have been told I can use FILTER_VALID_EMAIL but if I do I'm going to get the same error message saying you are already subscribed.

here is where i'm up to at the minute could anybody shed some light on my issue? would be much apreciated! :)

            <?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">&times;</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">&times;</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>
  • 写回答

1条回答 默认 最新

  • dongping6974 2014-01-30 00:18
    关注

    There is a syntax error. (missing the letters ATE to finish what's supposed to read as VALIDATE)

    You need to use: filter_var( $subsc_email , FILTER_VALIDATE_EMAIL )

    Instead of what you presently have:

    filter_var( $subsc_email , FILTER_VALID_EMAIL )
                               ---Missing -^ (ATE)
    

    Replace with the following:

    ( ! filter_var( $subsc_email , FILTER_VALIDATE_EMAIL ))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集