search
HomeBackend DevelopmentPHP ProblemHow does PHP+Mysql implement database addition, deletion, modification and query?

How does PHP+Mysql implement database addition, deletion, modification and query?

Jul 17, 2020 pm 02:02 PM
phpAdd, delete, modify and checkdatabase

PHP Mysql method to implement database addition, deletion, modification and query: 1. Create the entry file [index.html] to connect to the database and query data; 2. Click the Add button and add data through [addnews.html]; 3. Click Delete button, delete through the server file [action-del.php].

How does PHP+Mysql implement database addition, deletion, modification and query?

PHP Mysql method to implement database addition, deletion, modification and query:

1. Query the database

1.1. Create the file dbconfig.php and save the constants

<?php  
define("HOST","localhost");  
define("USER","root");  
define("PASS","********");
define("DBNAME","news");

1.2. Create the entry file index.html (connect to the database and query data)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>新闻后台管理系统</title>
</head>
<style type="text/css">
.wrapper {width: 1000px;margin: 20px auto;}
h2 {text-align: center;}
.add {margin-bottom: 20px;}
.add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;}
td {text-align: center;}
</style>
<body>
<div>
<h2 id="新闻后台管理系统">新闻后台管理系统</h2>
<div>
<a href="addnews.html">增加新闻</a>
</div>
<table width="960" border="1">
<tr>
<th>ID</th>
<th>标题</th>
<th>关键字</th>
<th>作者</th>
<th>发布时间</th>
<th>内容</th>
<th>操作</th>
</tr>
<?php
                // 1.导入配置文件
                require "dbconfig.php";
                // 2. 连接mysql
                $link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
                // 选择数据库
                mysql_select_db(DBNAME,$link);
                // 编码设置
                mysql_set_charset(&#39;utf8&#39;,$link);
// 3. 从DBNAME中查询到news数据库,返回数据库结果集,并按照addtime降序排列  
$sql = &#39;select * from news order by id asc&#39;;
                // 结果集
                $result = mysql_query($sql,$link);
                // var_dump($result);die;
// 解析结果集,$row为新闻所有数据,$newsNum为新闻数目
$newsNum=mysql_num_rows($result);  
for($i=0; $i<$newsNum; $i++){
$row = mysql_fetch_assoc($result);
echo "<tr>";
echo "<td>{$row[&#39;id&#39;]}</td>";
echo "<td>{$row[&#39;title&#39;]}</td>";
echo "<td>{$row[&#39;keywords&#39;]}</td>";
echo "<td>{$row[&#39;autor&#39;]}</td>";
echo "<td>{$row[&#39;addtime&#39;]}</td>";
echo "<td>{$row[&#39;content&#39;]}</td>";
echo "<td>
<a href=&#39;javascript:del({$row[&#39;id&#39;]})&#39;>删除</a>
<a href=&#39;editnews.php?id={$row[&#39;id&#39;]}&#39;>修改</a>
  </td>";
echo "</tr>";
}
// 5. 释放结果集
mysql_free_result($result);
mysql_close($link);
?>
</table>
</div>
<script type="text/javascript">
function del (id) {
if (confirm("确定删除这条新闻吗?")){
window.location = "action-del.php?id="+id;
}
}
</script>
</body>
</html>

The page is as shown:

How does PHP+Mysql implement database addition, deletion, modification and query?

#2. Add news

2.1 Click the Add button to go through the pageaddnews.htmlAdd data

<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>添加新闻</title>  
</head>
<style type="text/css">
form{
margin: 20px;
}
</style>
<body>
<form action="action-addnews.php" method="post">  
    <label>标题:</label><input type="text" name="title">  
    <label>关键字:</label><input type="text" name="keywords">  
    <label>作者:</label><input type="text" name="autor">  
    <label>发布时间:</label><input type="date" name="addtime">  
    <label>内容:</label><input type="text" name="content">  
    <input type="submit" value="提交">  
</form>  
</body>  
</html>

2.2 Create a server file to handle adding newsaction-addnews.php

<?php
// 处理增加操作的页面 
require "dbconfig.php";
// 连接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME,$link);
// 编码设置
mysql_set_charset(&#39;utf8&#39;,$link);
// 获取增加的新闻
$title = $_POST[&#39;title&#39;];
$keywords = $_POST[&#39;keywords&#39;];
$autor = $_POST[&#39;autor&#39;];
$addtime = $_POST[&#39;addtime&#39;];
$content = $_POST[&#39;content&#39;];
// 插入数据
mysql_query("INSERT INTO news(title,keywords,autor,addtime,content) VALUES (&#39;$title&#39;,&#39;$keywords&#39;,&#39;$autor&#39;,&#39;$addtime&#39;,&#39;$content&#39;)",$link) or die(&#39;添加数据出错:&#39;.mysql_error()); 
header("Location:demo.php");

3. Delete News

Click the delete button and delete it through the server fileaction-del.php

<?php
// 处理删除操作的页面 
require "dbconfig.php";
// 连接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME,$link);
// 编码设置
mysql_set_charset(&#39;utf8&#39;,$link);
$id = $_GET[&#39;id&#39;];
//删除指定数据  
mysql_query("DELETE FROM news WHERE id={$id}",$link) or die(&#39;删除数据出错:&#39;.mysql_error()); 
// 删除完跳转到新闻页
header("Location:demo.php");

4. Modify news

4.1 Click the modify button and jump to the file editnews.php for modification

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改新闻</title>
</head>
<body>
<?php
    require "dbconfig.php";
    $link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
    mysql_select_db(DBNAME,$link);
    mysql_set_charset(&#39;utf8&#39;,$link);
    
    $id = $_GET[&#39;id&#39;];
    $sql = mysql_query("SELECT * FROM news WHERE id=$id",$link);
    $sql_arr = mysql_fetch_assoc($sql); 
?>
<form action="action-editnews.php" method="post">
    <label>新闻ID: </label><input type="text" name="id" value="<?php echo $sql_arr[&#39;id&#39;]?>">
    <label>标题:</label><input type="text" name="title" value="<?php echo $sql_arr[&#39;title&#39;]?>">
    <label>关键字:</label><input type="text" name="keywords" value="<?php echo $sql_arr[&#39;keywords&#39;]?>">
    <label>作者:</label><input type="text" name="autor" value="<?php echo $sql_arr[&#39;autor&#39;]?>">
    <label>发布时间:</label><input type="date" name="addtime" value="<?php echo $sql_arr[&#39;addtime&#39;]?>">
    <label>内容:</label><input type="text" name="content" value="<?php echo $sql_arr[&#39;content&#39;]?>">
    <input type="submit" value="提交">
</form>
</body>
</html>

4.2 Through the server file action-editnews.php Modification processing

Modification processing through the server file action-editnews.php

<?php
// 处理编辑操作的页面 
require "dbconfig.php";
// 连接mysql
$link = @mysql_connect(HOST,USER,PASS) or die("提示:数据库连接失败!");
// 选择数据库
mysql_select_db(DBNAME,$link);
// 编码设置
mysql_set_charset(&#39;utf8&#39;,$link);
// 获取修改的新闻
$id = $_POST[&#39;id&#39;];
$title = $_POST[&#39;title&#39;];
$keywords = $_POST[&#39;keywords&#39;];
$autor = $_POST[&#39;autor&#39;];
$addtime = $_POST[&#39;addtime&#39;];
$content = $_POST[&#39;content&#39;];
// 更新数据
mysql_query("UPDATE news SET title=&#39;$title&#39;,keywords=&#39;$keywords&#39;,autor=&#39;$autor&#39;,addtime=&#39;$addtime&#39;,content=&#39;$content&#39; WHERE id=$id",$link) or die(&#39;修改数据出错:&#39;.mysql_error()); 
header("Location:demo.php");

Related learning recommendations:PHP programming from entry to proficiency

The above is the detailed content of How does PHP+Mysql implement database addition, deletion, modification and query?. 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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),