PHP writes MySQL data implementation code. PHP writes data to the MySQL database in three steps: 1. establish a connection relationship between PHP and MySQL 2. open MySQL database 3 and accept page data, PHP can write data directly to the MySQL database in steps 1 and 2 of the specified table:
1. establish a connection between PHP and MySQL
2. open the MySQL database
3. accept page data. input PHP Data to the specified table.
Steps 1 and 2 can directly use a database link File: conn. php
The code is as follows:
Mysql_connect ("localhost", "root", ""); // Connect to MySQL
Mysql_select_db ("hello"); // select a database
?>
Of course, the premise is that you have installed the WEB server, PHP, and MySQL, and created the MySQL table "cnbruce"
The three parameters of mysql_connect () are MySQL address, MySQL User name, and MySQL password.
Then, the data is transmitted through the WEB page, so that PHP writes data to the table specified by the MySQL database through SQL statements, such as creating a new file post. php
The code is as follows:
Require_once ("conn. php"); // reference the database link file
$ Uname = $ _ GET ['n']; // The GET method is passed as a URL parameter.
$ Psw = $ _ GET ['P'];
$ Psw = md5 ($ psw); // Use MD5 encryption directly
$ SQL = "insert into members (username, password) values ('$ uname',' $ psw ')";
Mysql_query ($ SQL); // use SQL statements to insert data
Mysql_close (); // Close the MySQL connection
Echo "successfully entered data ";
?>
Test Page: http: // localhost/post. php? N = cnbruce & p = i0514
You can insert the new data "cnbruce" to the username field and "i0514" to the password field into the members Table of MySQL database hello.
Connection 1, PHP and MySQL establish connection 2, open MySQL database 3, accept page data, PHP input to the specified table 1, 2 two steps can be straight...