PhpStorm 实现增删改查

旧城等待, 2021-09-18 00:00 701阅读 0赞

前几天有小伙伴问我能不能用php实现我的这一篇文章的效果 【ssm+easyui+sqlserver2008(增删改查实现)】

文章地址 : https://blog.csdn.net/k571039838k/article/details/77967780

这当然是没有问题的啦,因为是我是写java的,对于php也是一窍不通,所以我也算是php初学者了

老规矩 给大家看一看运行效果

作品展示

a 登录页面

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70

b 主界面

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 1

c 添加新闻

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 2

d 修改新闻

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 3

e 删除新闻

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 4

f 分页

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 5

g 模糊查询

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 6

以上就是咋们今天需要完成的作品了

准备工作

1、 下载PhpStorm 开发工具,网上很多百度下就OK

2、 下载 phpStudy 该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装,无须配置即可使用,是非常方便、好用的PHP调试环境·该程序不仅包括PHP调试环境,还包括了开发工具、开发手册等·总之学习PHP只需一个包。

对学习PHP的新手来说,WINDOWS下环境配置是一件很困难的事;对老手来说也是一件烦琐的事。因此无论你是新手还是老手,该程序包都是一个不错的选择。

项目结构

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2s1NzEwMzk4Mzhr_size_16_color_FFFFFF_t_70 7

重点介绍

1 、dbUtils.php (链接数据库工具类及的通用方法)

  1. <?php
  2. /**
  3. * 类说明 : 连接数据库工具类
  4. * User: 刘明昆
  5. * Date: 2019/1/21
  6. * Time: 8:53
  7. */
  8. header("Content-Type:text/html;charset=utf-8");
  9. //定义连接数据库的类,并返回链接后的资源
  10. //实现单例模式
  11. //执行普通增删改非返回结果集语句
  12. //执行select语句并可以返回3种类型的数据
  13. //单行结果(一维数组),多行结果(二维数组)
  14. //单行单例(单个数据)
  15. class mysqlDB{
  16. public $host;
  17. public $port;
  18. public $username;
  19. public $password;
  20. public $charset;
  21. public $dbname;
  22. //连接结果
  23. private static $link;
  24. private $resource;
  25. //单例方法
  26. public static function getInstance(){
  27. if(!isset(self::$link)){
  28. self::$link = new self();
  29. }
  30. return self::$link;
  31. }
  32. //禁止new
  33. private function __construct(){
  34. $this->host = 'localhost';
  35. $this->port = '3306';
  36. $this->username = 'lmk';
  37. $this->password = '123qwe';
  38. $this->charset = 'utf8';
  39. $this->dbname = 'test';
  40. $this->connect();
  41. //设定连接编码
  42. $this->setCharset($this->charset);
  43. //设定数据库
  44. $this->selectDb($this->dbname);
  45. }
  46. //禁止clone
  47. private function __clone(){}
  48. public function connect(){
  49. // $this->resource = mysql_connect("$this->host:$this->port","$this->username","$this->password") or die("连接数据库失败");
  50. $this->resource=mysqli_connect("$this->host","$this->username","$this->password","$this->dbname","$this->port") or die("连接数据库失败");
  51. }
  52. public function setCharset($charset){
  53. mysqli_set_charset($this->resource,$charset);
  54. }
  55. public function selectDb($dbname){
  56. mysqli_select_db($this->resource,$dbname);
  57. }
  58. /**
  59. * 执行最基本sql语句
  60. * @param 返回错误代码
  61. * @return 返回执行结果
  62. */
  63. public function query($sql){
  64. //执行失败
  65. if(!$result = mysqli_query($this->resource,$sql)){
  66. echo "<br/>sql语句".$sql;
  67. echo "<br/出错信息>".mysqli_error($this->resource);
  68. echo "<br/出错代码>".mysqli_errno($this->resource);
  69. die();
  70. }
  71. return $result;
  72. }
  73. /**
  74. * 功能执行select语句,返回2维数组
  75. * 参数:$sql 字符串类型 select语句
  76. */
  77. public function getAll($sql){
  78. $result = $this -> query($sql);
  79. $arr = array();
  80. while($rec = mysqli_fetch_assoc($result)){
  81. $arr[] = $rec;
  82. }
  83. return $arr;
  84. }
  85. /**
  86. * 返回影响行数
  87. *
  88. */
  89. public function getupd($sql){
  90. $this -> query($sql);
  91. return mysqli_affected_rows($this);
  92. }
  93. /**
  94. * 功能:返回一行数据作为一维数组
  95. * 参数:$sql 字符串类型 select语句
  96. */
  97. public function getRow($sql){
  98. $result = $this -> query($sql);
  99. if($rec2 = mysqli_fetch_assoc($result)){
  100. //如果fetch出来有数据,返回一维数组
  101. return $rec2;
  102. }
  103. return false;
  104. }
  105. /**
  106. * 功能:返回select的第一行第一列
  107. * 参数:$sql 字符串类型 select语句
  108. */
  109. public function getOne($sql){
  110. $result = $this -> query($sql);
  111. $rec = mysqli_fetch_row($result);//返回下标为数字的数组
  112. if($result == false){
  113. return false;
  114. }
  115. return $rec[0];
  116. }
  117. }

2、login.php (登录页面)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>我的“书屋”</title>
  6. <link rel="stylesheet" href="css/index2.css" />
  7. <script type="text/javascript" src="jquery/jquery_3.3.1.min.js"></script>
  8. </head>
  9. <body>
  10. <img src="img/bgImg.jpg" class="bgImg" />
  11. <div class="content">
  12. <div class="bidTitle">我的“书屋”登录</div>
  13. <div class="logCon">
  14. <div class="line"><span>账号:</span>
  15. <input class="bt_input" type="text" id="username"/></div>
  16. <div class="line"><span>密码:</span>
  17. <input class="bt_input" type="text" id="pwd"/></div>
  18. <button type="button" class="logingBut" onclick="btlogin()">登录</button>
  19. </div>
  20. </div>
  21. </body>
  22. <script type="application/javascript">
  23. function btlogin(){
  24. //获取文本框值
  25. var username =$("#username").val();
  26. var pwd=$("#pwd").val();
  27. //访问登录接口
  28. console.log(username);
  29. $.ajax({
  30. type: "POST",
  31. url: "/MyDemo/LoginDao.php",
  32. dataType:"json",
  33. data: { username: username, pwd: pwd },
  34. success: function (data) {
  35. if(data.code==200){
  36. //200代表访问成功
  37. if("登录成功"==data.message){
  38. //登录成功,将数据传输到主页
  39. window.location.href="home.php?data="+data.data[0].username;
  40. // alert(data.data);
  41. }else{
  42. alert(data.message);
  43. }
  44. }else{
  45. alert("访问失败,请稍后重试")
  46. }
  47. }
  48. });
  49. }
  50. </script>
  51. </html>

3、loginDao.php (登录方法的实现)

  1. <?php
  2. /**
  3. * 类说明 :
  4. * User: 刘明昆
  5. * Date: 2019/1/24
  6. * Time: 13:56
  7. */
  8. header("Content-type: text/html; charset=utf-8");
  9. require 'dbUtils.php';
  10. require 'jsonMsg.php';
  11. //获取用户名和密码
  12. $username=$_POST['username'];
  13. $pwd=$_POST['pwd'];
  14. //获取数据库实例
  15. $link=mysqlDB::getInstance();
  16. //查询用户名或密码是否正确
  17. $sql="select * from admin_user where username='$username' and pwd='$pwd';";
  18. //执行sql
  19. $result=$link->query($sql);
  20. $arr=array();
  21. if(mysqli_num_rows($result)<1){
  22. return json(200,"用户名或密码错误",$arr);
  23. }else{
  24. while($row=mysqli_fetch_assoc($result)){
  25. $arr[]=$row;
  26. }
  27. return json(200,"登录成功",$arr);
  28. }

4、show.php (主页面)

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>首页</title>
  6. <!-- Bootstrap -->
  7. <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
  8. <!-- easyui -->
  9. <link rel="stylesheet" type="text/css" href="easyui/jquery-easyui-1.5/themes/gray/easyui.css"/>
  10. <link rel="stylesheet" type="text/css" href="easyui/jquery-easyui-1.5/themes/icon.css"/>
  11. <script type="text/javascript" src="easyui/jquery-easyui-1.5/jquery.min.js"></script>
  12. <script type="text/javascript" src="easyui/jquery-easyui-1.5/jquery.easyui.min.js"></script>
  13. <script type="text/javascript" src="easyui/jquery-easyui-1.5/locale/easyui-lang-zh_CN.js"></script>
  14. <!-- easyui扩展 -->
  15. <link rel="stylesheet" type="text/css" href="easyui/easyui-ext/icons/icon-standard.css"/>
  16. <script type="text/javascript" src="easyui/easyui-ext/icons/icon-standard.js"></script>
  17. <!-- 自定义js -->
  18. <script type="text/javascript" src="js/common.js"></script>
  19. <script type="text/javascript" src="js/extJs.js"></script>
  20. <!-- 自定义css -->
  21. <link rel="stylesheet" href="css/common.css">
  22. <script type="text/javascript">
  23. var url;
  24. function searchUser() {
  25. $("#dg").datagrid('load', {
  26. "title" : $("#title").val()
  27. });
  28. console.log("--------------"+ $("#title").val());
  29. }
  30. function openUserAddDialog() {
  31. $("#dlg").dialog("open").dialog("setTitle", "添加新闻信息");
  32. url = "/MyDemo/addXinwen.php?action=add";
  33. }
  34. function openUserModifyDialog() {
  35. var selectedRows = $("#dg").datagrid("getSelections");
  36. if (selectedRows.length != 1) {
  37. $.messager.alert("系统提示", "请选择一条要编辑的数据!");
  38. return;
  39. }
  40. var row = selectedRows[0];
  41. $("#dlg").dialog("open").dialog("setTitle", "编辑新闻信息");
  42. $("#fm").form("load", row);
  43. url = "/MyDemo/addXinwen.php?action=update";
  44. }
  45. function saveUser() {
  46. $("#fm").form("submit", {
  47. url : url,
  48. onSubmit : function() {
  49. // if ($("#title").combobox("getValue") == "") {
  50. // $.messager.alert("系统提示", "请填写信息!");
  51. // return false;
  52. // }
  53. return $(this).form("validate");
  54. },
  55. success : function(result) {
  56. if (result==200) {
  57. $.messager.alert("系统提示", "添加成功");
  58. resetValue();
  59. $("#dlg").dialog("close");
  60. $("#dg").datagrid("reload");
  61. } else {
  62. $.messager.alert("系统提示", "添加失败");
  63. return;
  64. }
  65. }
  66. });
  67. }
  68. function resetValue() {
  69. $("#title").val("");
  70. $("#keywords").val("");
  71. $("#autor").val("");
  72. $("#content").val("");
  73. }
  74. function closeUserDialog() {
  75. $("#dlg").dialog("close");
  76. resetValue();
  77. }
  78. function deleteUser() {
  79. var selectedRows = $("#dg").datagrid("getSelections");
  80. if (selectedRows.length == 0) {
  81. $.messager.alert("系统提示", "请选择要删除的数据!");
  82. return;
  83. }
  84. var strIds = [];
  85. for ( var i = 0; i < selectedRows.length; i++) {
  86. strIds.push(selectedRows[i].id);
  87. }
  88. var ids = strIds.join(",");
  89. $.messager.confirm("系统提示", "您确定要删除这<font color=red>"
  90. + selectedRows.length + "</font>条数据吗?", function(r) {
  91. if (r) {
  92. $.post("/MyDemo/addXinwen.php?action=delete", {
  93. id : ids
  94. }, function(result) {
  95. if (result==200) {
  96. $.messager.alert("系统提示", "数据已成功删除!");
  97. $("#dg").datagrid("reload");
  98. } else {
  99. $.messager.alert("系统提示", "数据删除失败,请联系系统管理员!");
  100. }
  101. }, "json");
  102. }
  103. });
  104. }
  105. </script>
  106. </head>
  107. <body style="margin: 1px">
  108. <table id="dg" title="用户管理" class="easyui-datagrid" fitColumns="true"
  109. pagination="true" rownumbers="true"
  110. url="/MyDemo/showList.php" fit="true"
  111. toolbar="#tb">
  112. <thead>
  113. <tr>
  114. <th field="cb" checkbox="true" align="center"></th>
  115. <th field="id" width="50" align="center">编号</th>
  116. <th field="title" width="50" align="center">标题</th>
  117. <th field="keywords" width="50" align="center">关键字</th>
  118. <th field="autor" width="50" align="center">作者</th>
  119. <th field="addtime" width="50" align="center">时间</th>
  120. <th field="content" width="50" align="center">内容</th>
  121. </tr>
  122. </thead>
  123. </table>
  124. <div id="tb">
  125. <a href="javascript:openUserAddDialog()" class="easyui-linkbutton"
  126. iconCls="icon-add" plain="true">添加</a> <a
  127. href="javascript:openUserModifyDialog()" class="easyui-linkbutton"
  128. iconCls="icon-edit" plain="true">修改</a> <a
  129. href="javascript:deleteUser()" class="easyui-linkbutton"
  130. iconCls="icon-remove" plain="true">删除</a>
  131. <div>
  132. 标题: <input type="text" id="title" size="20"
  133. onkeydown="if(event.keyCode == 13)searchUser()" /> <a
  134. href="javascript:searchUser()" class="easyui-linkbutton"
  135. iconCls="icon-search" plain="true">查询</a>
  136. </div>
  137. <div id="dlg-buttons">
  138. <a href="javascript:saveUser()" class="easyui-linkbutton"
  139. iconCls="icon-ok">保存</a> <a href="javascript:closeUserDialog()"
  140. class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
  141. </div>
  142. <div id="dlg" class="easyui-dialog"
  143. style="width: 730px;height:280px;padding:10px 10px;" closed="true"
  144. buttons="#dlg-buttons">
  145. <form method="post" id="fm">
  146. <table cellspacing="8px;">
  147. <tr>
  148. <input type="hidden" id="id" name="id"/>
  149. <td>标题:</td>
  150. <td><input type="text" id="title" name="title"
  151. class="easyui-validatebox" required="true" /> <span
  152. style="color: red">*</span>
  153. </td>
  154. <td> </td>
  155. <td>关键词:</td>
  156. <td><input type="text" id="keywords" name="keywords"
  157. class="easyui-validatebox" required="true" /> <span
  158. style="color: red">*</span>
  159. </td>
  160. </tr>
  161. <tr>
  162. <td>作者:</td>
  163. <td><input type="text" id="autor" name="autor"
  164. class="easyui-validatebox" required="true" /> <span
  165. style="color: red">*</span>
  166. </td>
  167. <td> </td>
  168. <td>内容:</td>
  169. <td><input type="text" id="content" name="content"
  170. class="easyui-validatebox" required="true" /> <span
  171. style="color: red">*</span>
  172. </td>
  173. </tr>
  174. </table>
  175. </form>
  176. </div>
  177. </body>
  178. </html>

5、showList.php (用于处理主页面的分页和查询操作)

  1. <?php
  2. /**
  3. * 类说明 :
  4. * User: 刘明昆
  5. * Date: 2019-1-28
  6. * Time: 16:06
  7. */
  8. header("Content-type: text/html; charset=utf-8");
  9. require 'dbUtils.php';
  10. require 'jsonMsg.php';
  11. //获取数据库实例
  12. $link=mysqlDB::getInstance();
  13. $page=$_POST['page'];
  14. $rows=$_POST['rows'];
  15. if(!isset($_POST['title'])){
  16. $pages=($page-1)*10;
  17. //分页查询
  18. $sql=" SELECT * FROM admin limit $pages,$rows;";
  19. //查询总记录数
  20. $sql2="select count(*) as sum from admin;";
  21. //执行sql
  22. $result=$link->query($sql);
  23. //执行
  24. $result2=$link->query($sql2);
  25. $res = mysqli_fetch_array($result2);
  26. $count = $res['sum'];
  27. $arr=array();
  28. if(mysqli_num_rows($result)<1){
  29. return jsonlist(0,$arr);
  30. }else{
  31. while($row=mysqli_fetch_assoc($result)){
  32. $arr[]=$row;
  33. }
  34. return jsonlist($arr,$count);
  35. }
  36. }else{
  37. $title=$_POST['title'];
  38. $pages=($page-1)*10;
  39. //分页查询
  40. $sql=" SELECT * FROM admin where title like '%$title%' limit $pages,$rows;";
  41. //查询总记录数
  42. $sql2="select count(*) as sum from admin where title like '%$title%';";
  43. //执行sql
  44. $result=$link->query($sql);
  45. //执行
  46. $result2=$link->query($sql2);
  47. $res = mysqli_fetch_array($result2);
  48. $count = $res['sum'];
  49. $arr=array();
  50. if(mysqli_num_rows($result)<1){
  51. return jsonlist(0,$arr);
  52. }else{
  53. while($row=mysqli_fetch_assoc($result)){
  54. $arr[]=$row;
  55. }
  56. return jsonlist($arr,$count);
  57. }
  58. }

6、addXinwen.php (用于处理主页面的增删改操作)

  1. <?php
  2. /**
  3. * 类说明 :
  4. * User: 刘明昆
  5. * Date: 2019-1-28
  6. * Time: 16:33
  7. */
  8. header("Content-type: text/html; charset=utf-8");
  9. require 'dbUtils.php';
  10. require 'jsonMsg.php';
  11. $action = $_GET['action'];
  12. switch ($action) {
  13. case "add":
  14. //获取要添加的内容
  15. $title = $_POST['title'];
  16. $keywords = $_POST['keywords'];
  17. $autor = $_POST['autor'];
  18. $content = $_POST['content'];
  19. //获取数据库实例
  20. $link = mysqlDB::getInstance();
  21. //执行添加操作
  22. $sql = "insert into admin(title,keywords,autor,addtime,content) values('$title','$keywords'
  23. ,'$autor',(SELECT sysdate()),'$content');";
  24. //执行sql
  25. $result = $link->query($sql);
  26. // 如果影响行数>=1,则判断添加成功,否则失败
  27. if ($result >= 1) {
  28. // return msgtxt(200,"添加成功");
  29. return showmsgtxt(200);
  30. } else {
  31. // return msgtxt(500,"添加失败");
  32. return showmsgtxt(500);
  33. }
  34. break;
  35. case "update":
  36. //获取要修改的内容
  37. $title = $_POST['title'];
  38. $keywords = $_POST['keywords'];
  39. $autor = $_POST['autor'];
  40. $content = $_POST['content'];
  41. $id = $_POST['id'];
  42. //获取数据库实例
  43. $link = mysqlDB::getInstance();
  44. //执行添加操作
  45. $sql = "update admin set title='$title',keywords='$keywords',autor='$autor',content='$content' where id='$id'";
  46. //执行sql
  47. $result = $link->query($sql);
  48. // 如果影响行数>=1,则判断添加成功,否则失败
  49. if ($result >= 1) {
  50. // return msgtxt(200,"添加成功");
  51. return showmsgtxt(200);
  52. } else {
  53. // return msgtxt(500,"添加失败");
  54. return showmsgtxt(500);
  55. }
  56. break;
  57. case "delete";
  58. $id = $_POST['id'];
  59. //获取数据库实例
  60. $link = mysqlDB::getInstance();
  61. //执行添加操作
  62. $sql = "delete from admin where id in ($id);";
  63. //执行sql
  64. $result = $link->query($sql);
  65. // 如果影响行数>=1,则判断添加成功,否则失败
  66. if ($result >= 1) {
  67. // return msgtxt(200,"添加成功");
  68. return showmsgtxt(200);
  69. } else {
  70. // return msgtxt(500,"添加失败");
  71. return showmsgtxt(500);
  72. }
  73. break;
  74. }

以上就是我为大家带来的一个简单的新闻管理系统,请大家多多指教

附Demo下载地址

发表评论

表情:
评论列表 (有 0 条评论,701人围观)

还没有评论,来说两句吧...

相关阅读