How to modify sql in php: 1. Connect to the database and query the data; 2. Add data through the page addnews.html; 3. Through "mysql_query("UPDATE news SET title='$title'...) ” statement can be modified to update the data.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
How to modify sql in php?
PHP Mysql implements database addition, deletion, modification and query
PHP and Mysql can perform simple addition, deletion, modification and query on the database. This article introduces the news Backend management of the list.
Project address
https://github.com/caochangkui/php-mysql-test
Mysql database creation
Create A news list database:
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 database, 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 class="wrapper"> <h2 id="新闻后台管理系统">新闻后台管理系统</h2> <div class="add"> <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 , add data through the page addnews.html
nbsp;html> <meta> <title>添加新闻</title> <style> form{ margin: 20px; } </style>
2.2 Create the server-side file action-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, Delete through the server-side file action-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
nbsp;html> <meta> <title>修改新闻</title> <?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); ?>
4.2 Modify through the server-side file action-editnews.php
Modify through the server-side file action-editnews.php [Recommended learning: "PHP Video Tutorial"]
<?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");
The above is the detailed content of How to modify sql in php. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

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
