失败 php_PHP+Mysql 实现数据库增删改查

PHP和Mysql可以对数据库进行简单的增删改查,本文介绍了新闻列表的后台管理。

项目地址

https://github.com/caochangkui/php-mysql-test

Mysql数据库创建

创建一个新闻列表的数据库:

abdb494aeea617d21dfb146d85a95011.png

1. 查询数据库

1.1. 创建文件dbconfig.php,保存常量

<?php   
define("HOST","localhost");
define("USER","root");
define("PASS","********");
define("DBNAME","news");

1.2. 创建入口文件index.html(连接数据库、查询数据)

span style="transition: all 0.4s ease 0s;">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>IDth>
<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 $row = mysql_fetch_assoc($result);echo "";echo "{$row['id']}";echo "{$row['title']}";echo "{$row['keywords']}";echo "{$row['autor']}";echo "{$row['addtime']}";echo "{$row['content']}";echo ")'>删除'>修改";echo "";
}// 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>

页面如图:dc70e73bc02c2cd1bb2c9216027e4a5c.png

2. 增加新闻

2.1 点击增加按钮,通过页面addnews.html添加数据

span style="transition: all 0.4s ease 0s;">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>

2.2 创建处理增加新闻的服务端文件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. 删除新闻

点击删除按钮,通过服务端文件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. 修改新闻

4.1 点击修改按钮,跳转到文件editnews.php进行修改处理

span style="transition: all 0.4s ease 0s;">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 通过服务端文件action-editnews.php进行修改处理

通过服务端文件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 transition: all 0.4s ease 0s;">$title',keywords='$keywords',autor='$autor',addtime='$addtime',content='$content' WHERE id=$id",$link) or die('修改数据出错:'.mysql_error());
header("Location:demo.php");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值