PHP writes data to a MySQL database in three steps:
1,php and MySQL establish a connection relationship
2. Open MySQL Database
3, accept the page data, PHP input into the specified table
1, 22 step can directly use a database link file: conn.php
Copy CodeThe code is as follows:
mysql_connect ("localhost", "root", "");//Connect MySQL
mysql_select_db ("Hello");//Select Database
?>
Of course, the premise is that the Web server, PHP and MySQL are installed, and the MySQL table "Cnbruce" is built
Mysql_connect () Three parameters are MySQL address, MySQL user name and MySQL password, respectively
Then the data is passed through the Web page, allowing PHP to write data to the table specified in the MySQL database via SQL statements, such as a new file post.php
Copy CodeThe code is as follows:
Require_once ("conn.php");//reference Database link file
$uname = $_get[' n '];//get method passed for 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 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, "i0514" to the password field from the members table of the MySQL database hello
http://www.bkjia.com/PHPjc/320202.html www.bkjia.com true http://www.bkjia.com/PHPjc/320202.html techarticle PHP Write data to the MySQL database has three steps: 1,php and MySQL to establish a connection relationship 2, open MySQL database 3, accept page data, PHP input into the specified table 1, 22 steps can be straight ...