A simple automatic mail delivery system (II)

Source: Internet
Author: User
Tags implement insert connect mysql variables php and php and mysql mysql database
Here is an introduction to PHP and MySQL combined practical.

Basically, you can say that PHP is a middle tier between the backend database and the foreground browser, passing commands between them. This approach greatly enhances the likelihood of interaction and can be easily used in voting systems, other dynamic user input and personalization sites.

To implement this interaction, you first need to implement a connection to the MySQL database, which you can use to implement:
Syntax: int mysql_connect (string hostname, string Username, string password);

hostname-the host name where the database server is running.
Username-the name of the user to connect to the database server.
· Password-user password. The password set to connect to the MySQL database.
If the connection succeeds, the function returns a positive integer if the failure returns a negative number.

All orders, as usual, must be placed between "the" and "?>".

Let's continue with our project and let's assume that the following data tables are built with MySQL:

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

mysql> CREATE TABLE Information (
-> name VARCHAR (25),
-> Email VARCHAR (25),
-> Choice VARCHAR (8));

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

Now let's assume we want to insert the user's information into this database, we can modify the Email.php3 script to implement, modify the following:

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

?
/* This script will use variables passed from the moreinfo.html file. */

/* Declaration of some relevant variables * *

$hostname = "devshed";
$username = "MyUserName";
$password = "MyPassword";
$dbName = "Mydbname";

/* Use MySQL to create the data table access information * *
$userstable = "Information";

/* Site administrator's email address * *
$adminaddress = "administration@buycorn.com";

/* Connect to Database * *
Mysql_connect ($hostname, $username, $password) or DIE ("Unable to CONNECT to database");

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

PRINT "<CENTER>";
PRINT "Hello, $name."
PRINT "<BR><BR>";
PRINT "Thank for your interest.<br><br>";
PRINT "We'll send information to $email, and have noted so you like $preference";
PRINT "</CENTER><BR><BR>";

* * Send the message/
Mail ("$email", "Your Request for Information",
"$namenThank for your Interest!n
We sell fresh corn daily over the internet!
Place your in 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. ");

/* Insert the data into the datasheet/*
$query = "INSERT into $userstable VALUES (' $name ', ' $email ', ' $preference ')";
$result = mysql_query ($query);

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

/* Close the connection to the database * *
Mysql_close ();
?>

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

Some places to note:

1, the variables declared at the beginning of the script are for function mysql_connect (). We can also give these values directly in the function, however, considering the development of the project, these values should be placed in a separate file and transferred (#include).
2. Syntax: int mysql_select_db (string database_name, int link_identifier);
database_name must be the name of the database on the server.
Link_identifier (optional) is a connection that sends a request to the database server based on this.
• The return value is True/false
3. Syntax: int mysql_query (string query, int link_identifier);
query sends SQL commands to the MySQL server.
Link_identifier (optionally) indicates the connection from which SQL commands are sent to the database server.
• The return value is an integer, a positive number indicates success, and a negative number indicates failure.
4. Syntax: int mysql_close (int link_identifier);
Link_indentifier is the same as above
• The return value is an integer, a positive number indicates success, and a negative number indicates failure.

In the next article, I'll explain how to output data from MySQL.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.