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 to the specified table
1, 22 steps can directly use a database link file can: conn.php
| The code is as follows |
|
<?php 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 established.
Three parameters in mysql_connect () are MySQL address, MySQL username and mysql password respectively
Then it is through the Web page to pass data, so that PHP through SQL statements to write data to the MySQL database specified tables, such as new files
post.php
| The code is as follows |
|
<?php Require_once ("conn.php");//referencing Database link file $uname = $_get[' n '];//get method is passed to the URL parameter $pwd = $_get[' P ']; $pwd =md5 ($PWD);//Direct use of MD5 encryption $sql = "INSERT into Mythroad (Username,password) VALUES (' $uname ', ' $pwd ')"; mysql_query ($sql);//Borrow SQL statement to insert data Mysql_close ()//close MySQL connection echo "Successful data entry"; ?> |
You can insert new data "mythroad" to Username field, "mythroad" to Password field in the members table of the MySQL database hello
If you use form post we can use post to accept, see Update data below
Example:
| code is as follows |
&nbs P; |
| <?php $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 update the data successfully! "; } else { echo failed to update data:. Mysql_error (); } ? |