Home >Backend Development >PHP Problem >How to modify data in php mysql

How to modify data in php mysql

藏色散人
藏色散人Original
2020-08-29 09:30:074189browse

php Mysql method to modify data: First open the corresponding PHP file; then use the UPDATE command to modify the MySQL data table data. The syntax is "UPDATE table_name SET field1=new-value1, field2=new-value2".

How to modify data in php mysql

Recommended: "PHP Video Tutorial"

MySQL UPDATE update

If we need to modify or update the data in MySQL, we can use the SQL UPDATE command to operate.

Syntax

The following is the general SQL syntax for the UPDATE command to modify MySQL data table data:

UPDATE table_name SET field1=new-value1, field2=new-value2
[WHERE Clause]

You can update one or more fields at the same time.

You can specify any condition in the WHERE clause.

You can update data simultaneously in a single table.

The WHERE clause is very useful when you need to update data in a specified row in the data table.

Update data through the command prompt

Below we will use the WHERE clause in the SQL UPDATE command to update the specified data in the runoob_tbl table:

Example

The following example will update the runoob_title field value with runoob_id 3 in the data table:

SQL UPDATE statement:

mysql> UPDATE runoob_tbl SET runoob_title='学习 C++' WHERE runoob_id=3;
Query OK, 1 rows affected (0.01 sec)
 
mysql> SELECT * from runoob_tbl WHERE runoob_id=3;
+-----------+--------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+--------------+---------------+-----------------+
| 3         | 学习 C++   | RUNOOB.COM    | 2016-05-06      |
+-----------+--------------+---------------+-----------------+
1 rows in set (0.01 sec)

From the results, runoob_title with runoob_id 3 has been modified.

Use PHP script to update data

The function mysqli_query() is used in PHP to execute SQL statements. You can use or not use the WHERE clause in the SQL UPDATE statement.

Note: Do not use the WHERE clause to update all the data in the data table, so be careful.

This function has the same effect as executing SQL statements in the mysql> command prompt.

Example

The following example will update the data of the runoob_title field with runoob_id 3.

MySQL UPDATE statement test:

<?php
$dbhost = &#39;localhost&#39;;  // mysql服务器主机地址
$dbuser = &#39;root&#39;;            // mysql用户名
$dbpass = &#39;123456&#39;;          // mysql用户名密码
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die(&#39;连接失败: &#39; . mysqli_error($conn));
}
// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");
 
$sql = &#39;UPDATE runoob_tbl
        SET runoob_title="学习 Python"
        WHERE runoob_id=3&#39;;
 
mysqli_select_db( $conn, &#39;RUNOOB&#39; );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die(&#39;无法更新数据: &#39; . mysqli_error($conn));
}
echo &#39;数据更新成功!&#39;;
mysqli_close($conn);
?>

The above is the detailed content of How to modify data in php mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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