Php+mysql implementation of database additions and deletions

Source: Internet
Author: User

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 News

Click 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.php

Modification 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.