search
HomeBackend DevelopmentPHP ProblemHow to use php to add, delete, modify and query mysql?

How to use php to add, delete, modify and query mysql?

Jul 13, 2020 am 11:34 AM
mysqlphpAdd, delete, modify and check

How to add, delete, modify and query PHP to operate mysql: 1. Insert statement [insert into data table name (field 1, field 2,....) values ​​("value 1", "value 2",. .)]; 2. Delete statement [delete from indicates where field = 'condition'].

How to use php to add, delete, modify and query mysql?

How to add, delete, modify and query php to operate mysql:

1. Step 1: Go to www in wamp In the folder directory, create a test folder, and then create a message folder in the test folder to store each file that has been added, deleted, modified, and checked. (As shown in the figure)

How to use php to add, delete, modify and query mysql?

2. Step 2: Remember to turn on wamp, and then create conn.php in the www/test/message folder File, mainly used to connect php to mysql database.

The code is as follows: (You can also see the picture)

// 连接数据库:mysqli_connect(服务器名,用户名,密码,数据库名)
$conn = mysqli_connect("localhost","root",'','user');
// 设置插入数据编码为utf8,不设置的话会出现乱码
mysqli_query($conn,"set names utf8");

How to use php to add, delete, modify and query mysql?

3. Digression: Here I created a user database, you can also create what you need For databases, the key thing is that you need to know what the fields in your database look like.

SQL statement created by user database: (You only need to copy this part of the database code to the database statement to create this table. The first sentence is to create a database called user and create a message data table in it. .)

create database user; 
CREATE TABLE `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `content` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `who` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;

How to use php to add, delete, modify and query mysql?

4. PHP operates Mysql to add data

The first step: Add data (add, delete, modify) During the search process, you must use the connection to the database)

First you have to create an index.php in www/test/message/ to implement the operation php to add data to the mysql database. (You can create it as shown in the file path in the introduction)

Key place: Insert statement "insert into data table name (field 1, field 2,....) values ​​("value 1", "Value 2",....)"

<?php
 include "conn.php";//引用数据库连接文件
//关键的地方是sql语句的书写,一定不能写错,要对应前一个数据表的字段对应插入对应的值,然后实现插入数据。
$sql = "insert into message(title,content,who) values(&#39;留言标题&#39;,&#39;留言内容&#39;,&#39;留言人&#39;)";
$result = mysqli_query($conn,$sql);//处理数据语句,这句很重要!!
if($result){
echo "发布留言成功!";
}else{
echo "发布留言失败!";
}

Note: When you want to run this part of the code, you need to turn on wamp, and then enter the path in the browser : localhost/test/message/index.php, and then check it.

The following code runs in the same way!

How to use php to add, delete, modify and query mysql?

5. PHP operates Mysql to delete data

Step 2: Implementation of adding data (process of adding, deleting, modifying and checking You must use the method to connect to the database)

First you have to create a shanchu.php in www/test/message/ to implement the operation php to delete the data of the mysql database. (You can create it as shown in the file path in the introduction)

Key point: delete the statement "delete from indicates where field = 'condition' "

<?php
 include "conn.php";//引用数据库连接文件
 // 删除留言,关键在于sql语句的改变
 $sql = "delete from message where id=&#39;1&#39;";//删除id=1的数据
 $result = mysqli_query($conn,$sql);//这句是执行sql语句,很重要!
 if($result){
  echo "删除成功!<a href=&#39;message.php&#39;>返回</a>";
 }else{
  echo "删除失败!<a href=&#39;message.php&#39;>返回</a>";
 }

Note: You need to When running this part of the code, you need to turn on wamp, then enter the path :localhost/test/message/shanchu.php in the browser, and then check it.

The following code runs in the same way!

How to use php to add, delete, modify and query mysql?

6. PHP operates Mysql to realize data search

Step 3: Implementation of data search (process of adding, deleting, modifying and checking You must use the method to connect to the database)

First you have to create a message.php in www/test/message/ to implement the operation PHP to delete the data of the mysql database. (You can create it as shown in the file path in the introduction)

Key place: Find the statement "select * from table name where condition (optional)"

Code part:

<?php 
include "conn.php";//引用数据库连接文件
 $sql = "select * from message";//查询全部数据
 $result = mysqli_query($conn,$sql); //处理sql语句
 while($row = mysqli_fetch_array($result){
  echo $row[&#39;title&#39;];
  echo $row[&#39;content&#39;];
  echo $row[&#39;who&#39;];
 }

Note: When you want to run this part of the code, you need to enable wamp, then enter the path in the browser: localhost/test/message/message.php, and then check of.

The following code runs in the same way!

How to use php to add, delete, modify and query mysql?

7. PHP operates mysql to modify data

The third step: Implementation of modifying data (the process of adding, deleting, modifying and checking is Must be used to connect to the database)

First you have to create a xiugai.php in www/test/message/ to implement the operation php to delete the data of the mysql database. (You can create it as shown in the file path in the introduction)

Key place: Modify the statement "update table name set field 1='value 1', field 2='value 2', field 3=' Value 3' where field='condition'"

Code part:

<?php
 include "conn.php";//引用数据库连接文件
// 修改对应id数据到数据库,修改id=1的数据
$sql="update message set title=&#39;修改标题&#39;,content=&#39;修改内容&#39;,who=&#39;修改名&#39; where id=&#39;1&#39;";
$result = mysqli_query($conn,$sql);//处理数据库
if($result){
echo "修改留言成功!";
}else{
echo "修改留言失败!";
}

注意:你要运行这部分代码的时候,是要开启wamp后,然后在浏览器里面路径输入:localhost/test/message/xiugai.php,然后查看的。

How to use php to add, delete, modify and query mysql?

相关学习推荐:PHP编程从入门到精通

The above is the detailed content of How to use php to add, delete, modify and query 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
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools