


How does PHP+Mysql implement database addition, deletion, modification and query?
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].
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('utf8',$link); // 3. 从DBNAME中查询到news数据库,返回数据库结果集,并按照addtime降序排列 $sql = 'select * from news order by id asc'; // 结果集 $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['id']}</td>"; echo "<td>{$row['title']}</td>"; echo "<td>{$row['keywords']}</td>"; echo "<td>{$row['autor']}</td>"; echo "<td>{$row['addtime']}</td>"; echo "<td>{$row['content']}</td>"; echo "<td> <a href='javascript:del({$row['id']})'>删除</a> <a href='editnews.php?id={$row['id']}'>修改</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:
#2. Add news
2.1 Click the Add button to go through the pageaddnews.html
Add 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('utf8',$link); // 获取增加的新闻 $title = $_POST['title']; $keywords = $_POST['keywords']; $autor = $_POST['autor']; $addtime = $_POST['addtime']; $content = $_POST['content']; // 插入数据 mysql_query("INSERT INTO news(title,keywords,autor,addtime,content) VALUES ('$title','$keywords','$autor','$addtime','$content')",$link) or die('添加数据出错:'.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('utf8',$link); $id = $_GET['id']; //删除指定数据 mysql_query("DELETE FROM news WHERE id={$id}",$link) or die('删除数据出错:'.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('utf8',$link); $id = $_GET['id']; $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['id']?>"> <label>标题:</label><input type="text" name="title" value="<?php echo $sql_arr['title']?>"> <label>关键字:</label><input type="text" name="keywords" value="<?php echo $sql_arr['keywords']?>"> <label>作者:</label><input type="text" name="autor" value="<?php echo $sql_arr['autor']?>"> <label>发布时间:</label><input type="date" name="addtime" value="<?php echo $sql_arr['addtime']?>"> <label>内容:</label><input type="text" name="content" value="<?php echo $sql_arr['content']?>"> <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('utf8',$link); // 获取修改的新闻 $id = $_POST['id']; $title = $_POST['title']; $keywords = $_POST['keywords']; $autor = $_POST['autor']; $addtime = $_POST['addtime']; $content = $_POST['content']; // 更新数据 mysql_query("UPDATE news SET title='$title',keywords='$keywords',autor='$autor',addtime='$addtime',content='$content' WHERE id=$id",$link) or die('修改数据出错:'.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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.
