The two most common ways to save data to a MySQL database in PHP are the insert and update methods, and I'll show you how to save the MySQL data for beginners.
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
The code is as follows |
Copy Code |
mysql_connect ("localhost", "root", "");//Connect MySQL mysql_select_db ("Mythroad");//Select Database ?> |
Of course, the premise is that the Web server, PHP and MySQL are installed, and the MySQL table "Mythroad" 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 creating a new file
post.php
The code is as follows |
Copy Code |
Require_once ("conn.php");//reference Database link file $uname = $_get[' n '];//get method passed for URL parameter $pwd = $_get[' P ']; $pwd =md5 ($PWD);//use MD5 encryption directly $sql = "INSERT into Mythroad (Username,password) VALUES (' $uname ', ' $pwd ')"; mysql_query ($sql);//Use SQL statements to insert data Mysql_close ();//close MySQL connection echo "successfully entered data"; ?> |
Test page: Http://127.0.0.1/post.php?n=mythroad&p=mythroad
You can insert the new data "mythroad" to the username field, "mythroad" to the password field from the members table of the MySQL database hello
If we use the form post we can use post to accept, look at the update updated data under the surface
Example:
The code is as follows |
Copy Code |
$conn = @mysql_connect ("localhost", "root", "root123"); if (! $conn) { Die ("Connection Database failed:". Mysql_error ()); } mysql_select_db ("Test", $conn); mysql_query ("Set names ' GBK '"); $sql = "UPDATE user SET email = ' xiaoming@163.com ' WHERE username = ' Xiaoming '"; if (mysql_query ($sql, $conn)) { echo "Updated data successfully! "; } else { echo "Failed to update data:". Mysql_error (); } ?> |
http://www.bkjia.com/PHPjc/632911.html www.bkjia.com true http://www.bkjia.com/PHPjc/632911.html techarticle The two most common ways to save data to a MySQL database in PHP are the insert and update methods, and I'll show you how to save the MySQL data for beginners. PHP writes data to MySQL database with ...