我有一个简单的'即将推出'页面,我将订阅电子邮件,我打算插入到mysql数据库中。
之前我运行了代码,但现在回到它1-2周后,似乎有一些问题。
基本上只涉及2个文件:index.html
和subscribe.php
。 index.html
实际上是“即将推出”页面,它会调用subscribe.php
实际将电子邮件插入数据库,前提是它是有效的电子邮件,不是重复的等等。
subscribe.php
的代码如下。它真的很简单。
不要以前这个WAS工作!但是现在似乎在使用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';
}
}
答案 0 :(得分:3)
可能在你的php.ini中禁用了PDO,只需启用它。确保你有
extension=pdo.so
extension=pdo_mysql.so