Home > Backend Development > PHP Tutorial > PHP save data to Mysql database code example_PHP tutorial

PHP save data to Mysql database code example_PHP tutorial

WBOY
Release: 2016-07-13 10:46:44
Original
1016 people have browsed it

The two most commonly used methods of saving data to the mysql database in php are the insert and update methods. Let me introduce the mysql data saving method to beginners.

There are three steps for PHP to write data to the MySQL database:

1. Establish a connection between PHP and MySQL
2. Open the MySQL database
3. Accept the page data and enter it into the specified table with PHP
You can directly use a database link file in steps 1 and 2: conn.php

The code is as follows
 代码如下 复制代码

mysql_connect("localhost","root","");//连接MySQL
mysql_select_db("mythroad");//选择数据库
?>

Copy code



mysql_connect("localhost","root","");//Connect MySQL

mysql_select_db("mythroad");//Select database
代码如下 复制代码

require_once("conn.php");//引用数据库链接文件
$uname = $_GET['n'];//GET方法为URL参数传递
$pwd = $_GET['p'];
$pwd =md5($pwd );//直接使用MD5加密
$sql = "insert into mythroad(username,password) values ('$uname','$pwd')";
mysql_query($sql);//借SQL语句插入数据
mysql_close();//关闭MySQL连接
echo "成功录入数据";
?>

?>


Of course, the premise is that the WEB server, PHP and MySQL have been installed, and the MySQL table "mythroad" has been created The three parameters in mysql_connect() are MySQL address, MySQL username and MySQL password

Then the data is passed through the WEB page, and PHP writes the data into the table specified in the MySQL database through SQL statements, such as creating a new file

post.php

The code is as follows
 代码如下 复制代码

$conn = @mysql_connect("localhost","root","root123");
if (!$conn){
die("连接数据库失败:" . mysql_error());
}

mysql_select_db("test", $conn);
mysql_query("set names 'gbk'");

$sql = "UPDATE user SET email = 'xiaoming@163.com' WHERE username = '小明'";
if(mysql_query($sql,$conn)){
echo "更新数据成功!";
} else {
echo "更新数据失败:".mysql_error();
}
?>

Copy code
require_once("conn.php");//Reference database link file

$uname = $_GET['n'];//GET method passes URL parameters

$pwd = $_GET['p']; $sql = "insert into mythroad(username,password) values ​​('$uname','$pwd')"; mysql_query($sql);//Insert data using SQL statements mysql_close();//Close the MySQL connection echo "Data entered successfully"; ?> Test page: http://127.0.0.1/post.php?n=mythroad&p=mythroad You can insert new data "mythroad" into the members table of hello in the MySQL database into the username field and "mythroad" into the password field If you use form post, we can use post to accept it. Let’s see the update data below
Example:
The code is as follows Copy code
$conn = @mysql_connect("localhost","root","root123");<🎜> if (!$conn){<🎜> Die("Failed to connect to database: " . 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 "Data updated successfully!";<🎜> } else {<🎜> echo "Failed to update data:".mysql_error();<🎜> }<🎜> ?> http://www.bkjia.com/PHPjc/632911.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632911.htmlTechArticleThe two most commonly used methods of saving data to the mysql database in php are the insert and update methods. Let me explain below Beginners will learn how to save mysql data. PHP writes data to the MySQL database...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template