Home Backend Development PHP Tutorial 一个简单的自动发送邮件系统(二)

一个简单的自动发送邮件系统(二)

Jun 01, 2016 pm 02:29 PM

这里介绍php和MySQL结合起来实用。

  基本上,可以说php是介于后台数据库和前台浏览器的一个中间层,在他们之间传递命令。这种方式大大提高了交互的可能性,可以方便使用在投票系统,其他动态用户输入和个性化网站中。

  要想实现这种交互,首先必需实现和mysql数据库连接,可以使用这个命令实现:
  语法:int mysql_connect(string hostname, string username, string passWord); 

  ·hostname - 运行数据库服务器所在的主机名称。
  ·username - 连接到数据库服务器的用户名称。
  ·Password - 用户密码。the password set to connect to the MySQL database. 
   如果连接成功,函数返回一个正整数,如果失败返回一个负数。

   所有的命令,和往常一样,必需放置在"" 和 "?>"之间。
    
   让我们继续我们的工程,让我们假设用MySQL建立了以下的数据表:

----------------------------  
 
mysql> CREATE TABLE information (
  -> name VARCHAR (25),
  -> email VARCHAR (25),  
  -> choice VARCHAR (8) );

----------------------------  

  现在让我们假设我们要吧用户的信息插入到这个数据库中,我们可以通过修改email.php3脚本来实现,修改如下:

----------------------------  


/* 这个脚本将使用从moreinfo.html文件中传递过来的变量。 */

/* 声明一些相关的变量 */

$hostname = "devshed";
$username = "myusername";
$password = "mypassword";
$dbName = "mydbname";

/* 使用MySQL建立的数据表存取信息 */
$userstable = "information";

/* 网站管理员的邮件地址*/
$adminaddress = "administration@buycorn.com";

/* 与数据库连接*/
MYSQL_CONNECT($hostname,$username,$password) OR DIE("Unable to connect to database");

@mysql_select_db("$dbName") or die("Unable to select database"); 

PRINT "
";
PRINT "Hello, $name.";
PRINT "

";
PRINT "Thank you for your interest.

";
PRINT "We will send information to $email, and have noted that you like $preference";
PRINT "


";

/* 发送有关邮件*/
mail("$email", "Your request for information",
"$namenThank you for your interest!n
We sell fresh corn daily over the Internet!
Place your order at http://www.buycorn.com,
and receive a free package of $preference!");

mail("$adminaddress",
"Visitor request for info.",
"$name requested for information.n

The email address is $email. n The visitor prefers $preference.");

/* 将数据插入数据表中*/
$query = "INSERT INTO $userstable VALUES('$name','$email', '$preference')";
$result = MYSQL_QUERY($query);

PRINT "Your information has also been inserted into our database, for future reference.";

/* 关闭与数据库的连接*/
MYSQL_CLOSE();
?>

----------------------------  

一些注意的地方:

1、在脚本一开始声明的变量是为了函数MYSQL_CONNECT() 。我们也可以直接在函数中将这些值给出,可是,考虑工程的可发展性,这些值应该放在一个独立的文件中,用(#include)调入。
2、语法: int mysql_select_db(string database_name, int link_identifier);
·database_name 必需是在服务器上的数据库名。
·link_identifier(可选的) 是指明连接,基于此向数据库服务器发出请求。
·返回值为true/false
3、语法: int mysql_query(string query, int link_identifier);
·query 发送向mysql服务器的sql命令。
·link_identifier(可选择) 指明连接,基于此向数据库服务器发送sql命令。
·返回值为整数,正数表示成功了,负数表示失败。
4、语法: int mysql_close(int link_identifier); 
·link_indentifier 与上面相同
·返回值为整数,正数表示成功了,负数表示失败。

在下一篇文章中,我将给大家讲解如何从mysql中输出数据。 
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles