我是 PHP 和 MySql 的新手,我一直在尝试解决这个问题几个小时,但无济于事。我正在尝试创建一个邮件列表。填写表格并点击提交后,数据将不会出现在数据库表中。任何帮助表示赞赏。还不知道如何调试,所以我也没有收到错误。我一直在关注本教程http://www.youtube.com/watch?v=HL884ugSL8c
mail.inc.php 文件:
<?php
// adds information to the table
function add_user($firstname, $lastname, $email){
$firstname = mysql_real_escape_string($firstname);
$lastname = mysql_real_escape_string($lastname);
$email = mysql_real_escape_string($email);
$result = mysql_query("INSERT INTO 'users' ('firstname', 'lastname', 'email') VALUES ('{$firstname}','{$lastname}','{$email}')");
return ($result !== false) ? true : false;
}
//removes the given email address from the table
function remove_user($email){
$email = mysql_real_escape_string($email);
mysql_query("DELETE FROM 'users' WHERE 'email' = '{$email}'");
}
// sends a given message to all subscribed users
function mail_all($subject, $message, $headers){
$users = mysql_query("SELECT 'firstname', 'email' FROM 'users'");
while (($user = mysql_fetch_assoc($users)) !== false){
$body = "Hi, {$user['firstname']}\n\n{$message}\n\nUnsubscribe: ";
mail($user['email'], $subject, $body, $headers);
}
}
?>
注册.php 文件:
<?php
include('core/init.inc.php');
// checks if all forms have been filled !!!!!!!
if (isset($_POST['firstname]'], $_POST['lastname'], $_POST['email'])){
$errors = array();
if (preg_match('/^[a-z]+$/i', $_POST['firstname']) === 0){
$errors[] = 'Your first name should contain letters only.';
}
if (preg_match('/^[a-z]+$/i', $_POST['lastname']) === 0){
$errors[] = 'Your last name should contain letters only.';
}
if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'This email address does not appear to be valid';
}
if (empty($errors)){
add_user($_POST['firstname'], $_POST['lastname'], $_POST['email']);
}
}
?>
<form action="" method="post">
<p>
<label for="firstname">Firstname</label>
<input type="text" name="firstname" id="firstname" />
</p>
<p>
<label for="lastname">Lastname</label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
<label for="email">E-mail</label>
<input type="text" name="email" id="email" />
</p>
<p>
<input type="submit" value="Signup" />
</p>
</form>
init.inc.php 文件包含以下内容:
<?php
mysql_connect('localhost','wd','wd'); //not bothered
mysql_select_db('leedsattractions');
$path = dirname(__FILE__);
include("{$path}/inc/mail.inc.php");
?>
我已将 mail.inc.php 文件的其余部分添加到楼上的第一个嵌入文件中。