PHP and MySQL can make simple additions and deletions to the database, this article introduces the background management of the news list.
MySQL Database creation
Create a database of news lists:
1. Query Database 1.1. Create file dbconfig.php, save constants
<?php define("HOST","localhost"); define("USER","root"); define("PASS","********");define("DBNAME","news");
1.2. Create a Portal file index.html (connect database, query data)
<! DOCTYPE html>
Page
2. Added News 2.1 Click the Add button, adding data via page addnews.html<!DOCTYPE html>
2.2 Create a service-side file that handles increased news 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 NewsClick the Delete button to delete processing via the server 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 the news 4.1 Click the Modify button to jump to the file editnews.php for modification processing<! DOCTYPE html>
4.2 Modification processing via the server-side file action-editnews.phpModification processing via the server-side 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");
Php+mysql implementation of database additions and deletions