PHP Save Data code sample to MySQL database _php tutorial

Source: Internet
Author: User
Tags form post php and mysql
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 ...

  • Related Article

    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.