Table of Contents
Project address
Mysql database creation
1. Query the database
1.1. Create the file dbconfig.php and save the constants
1.2. Create the entry file index.html (connect to database, query data)
2. Add news
2.1 Click the Add button , add data through the page addnews.html
2.2 Create the server-side file action-addnews.php
3. Delete news
4. Modify news
4.1 Click the modify button and jump to the file editnews.php for modification
4.2 Modify through the server-side file action-editnews.php
Home Backend Development PHP Problem How to modify sql in php

How to modify sql in php

Nov 01, 2021 am 09:33 AM
php

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.

How to modify sql in php

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:

How to modify sql in php

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");
Copy after login

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>新闻后台管理系统</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(&#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>
Copy after login

The page is as shown:
How to modify sql in php

2. Add news

2.1 Click the Add button , add data through the page addnews.html

<!DOCTYPE html>  
<html>  
<head lang="en">  
    <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>
Copy after login

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(&#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");
Copy after login

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(&#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");
Copy after login

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>
Copy after login

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(&#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");
Copy after login

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!

Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

See all articles