(《Head First PHP & MySQL》学习记录)
输入姓名及邮箱提交可订阅,订阅后可输入邮箱取消订阅,可给所有订阅用户发送邮件。
订阅界面:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Add Email</title>
</head>
<body>
<h1>Add to our email list</h1>
<p>Enter your name and email and get our updatest information!</p>
<form method="post" action="email-list.php">
<label for="firstname">First Name: </label>
<input type="text" id="firstname" name="firstname" /><br />
<label for="lastname">Last Name:</label>
<input type="text" id="lastname" name="lastname" /><br />
<label for="email">Email: </label>
<input type="text" id="email" name="email" /><br />
<input type="submit" value="Add" />
</form>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Add email</title>
</head>
<body>
<h1>Add to our email list</h1>
<?php
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$dbc=mysqli_connect('127.0.0.1','root','12345678','maystudio') or die('Error connecting to MySQL server.');
$query="INSERT INTO email_list (first_name,last_name,email) VALUES ('$firstname','$lastname','$email')";
mysqli_query($dbc,$query) or die('Error query database.');
mysqli_close($dbc);
echo '<p>Thank you for subscription, we are not going to let you down!</p>';
echo '<a href="unsubscribe.html">Unsubscription</a>';
?>
</body>
</html>
取消订阅界面:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Unsubscribe Us</title>
</head>
<body>
<h1>Unsubscription</h1>
<p>We are so sorry to let you down.=(</p>
<form method="post" action="unsubscribe.php">
<label for="email">Enter your email address: </label>
<input type="text" id="email" name="email" /><br />
<input type="submit" value="Unsubscribe" />
</form>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Unsubscribe Us</title>
</head>
<body>
<h1>Unsubscription</h1>
<?php
$email=$_POST['email'];
$dbc=mysqli_connect('127.0.0.1','root','12345678','maystudio') or die('Error connecting to MySQL server.');
$query="DELETE FROM email_list WHERE email = '$email' ";
mysqli_query($dbc,$query) or die('Error querying database.');
mysqli_close($dbc);
echo 'Successful unsubscription!';
?>
</body>
</html>
发送邮件界面:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<h1>Send email to subscribers</h1>
<p>Maystudio's administrators only!</p>
<form method="post" action="send-email.php">
<label for="subject">Subject: </label><br />
<input type="text" id="subject" name="subject" /><br />
<label for="body">Body: </label><br />
<textarea id="body" name="body" rows="8" cols="60"></textarea><br />
<input type="submit" name="submit" value="Send" />
</form>
</body>
</html>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Send email</title>
</head>
<body>
<h1>Email sent</h1>
<?php
$from='changingvan@163.com';
$subject=$_POST['subject'];
$body=$_POST['body'];
$dbc=mysqli_connect('127.0.0.1','root','12345678','maystudio') or die('Error connecting to MySQL server.');
$query="SELECT * FROM email_list";
$result=mysqli_query($dbc,$query) or die('Error querying database.');
while($row=mysqli_fetch_array($result)){
$first_name=$row['first_name'];
$last_name=$row['last_name'];
$msg="Dear $first_name $last_name,\n $body";
$to=$row['email'];
mail($to,$subject,$msg,'From: '.$from);
echo 'Email sent to '.$to.'<br />';
}
mysqli_close($dbc);
?>
</body>
</html>