0

我有一个简单的“即将推出”页面,我在该页面上订阅了我打算插入 mysql 数据库的电子邮件。

我之前运行过代码,但现在在 1-2 周后回到它,似乎有一些问题。

基本上只涉及 2 个文件:index.htmlsubscribe.php. index.html是实际的“即将推出”页面,它要求subscribe.php将电子邮件实际插入数据库,前提是它是有效的电子邮件,不是重复的等等。

下面给出了代码subscribe.php。它真的是一个非常简单的代码。

不要以为这是以前的工作!但是,现在使用 PDO 的行似乎出现“找不到类 PDO ...”错误:

<?php

function isValidEmail( $email = null )
{
    return preg_match( "/^
    [\d\w\/+!=#|$?%{^&}*`'~-]
    [\d\w\/\.+!=#|$?%{^&}*`'~-]*@
    [A-Z0-9]
    [A-Z0-9.-]{0,61}
    [A-Z0-9]\.
    [A-Z]{2,6}$/ix", $email );
}

try {
    // Connect to the SQLite Database.
    $db = new PDO('mysql:host=hostnamehere;dbname=dbnamehere', 'usernamehere', 'passwordhere');
} catch(Exception $e) {
    die('connection_unsuccessful');
}

/* Check if table exists */
$db->exec('CREATE TABLE IF NOT EXISTS subscribers (email VARCHAR(255), time VARCHAR(255))');

/* Check if email has been posted */
if ( isset($_POST['email']) ) {

    /* Validate email */
    if ( isValidEmail($_POST['email']) ) {

        /* Check for duplication */
        $query = $db->prepare('SELECT COUNT(*) AS count FROM subscribers WHERE email = :email');  
        $query->execute(array(':email' => $_POST['email']));
        $result = $query->fetch();

        if ( $result['count'] == 0 ) { // E-mail is unique.

            $query = $db->prepare('INSERT INTO subscribers (email, time) VALUES (:email, :time)');  
            $query->execute(array('email' => $_POST['email'], 'time' => date('Y-m-d H:i:s')));

            /* Send mail notification */
            $to = 'newsubscriber@xyz.com'; // Email notified of the new subscription
            $subject = 'New subscriber';
            $message = 'Hi, you have one new subscriber. This is his/her e-mail address: ' . $_POST['email'] . '.';
            $headers = "From:" . $_POST['email'];
            mail($to,$subject,$message,$headers);

            echo 'successful';

        } else { // E-mail is already being used.
            echo 'already_subscribed';
        }

    } else {
        echo 'invalid_email';
    }

}
4

1 回答 1

3

可能在您的 php.ini 中禁用了 PDO,只需启用它即可。确保你有

extension=pdo.so
extension=pdo_mysql.so
于 2013-05-17T21:10:21.247 回答