zoukankan      html  css  js  c++  java
  • PHP----练习-----新闻管理----增删改查

                                                       练习-----新闻管理

    题目要求如下:

    做法:

    【1】建数据库

    【2】封装类文件--------DBDA.class.php

     1 <?php
     2 class DBDA
     3 {
     4     public $fuwuqi="localhost";  //服务器地址
     5     public $yonghuming="root";//用户名
     6     public $mima="";//密码    
     7     
     8     public $dbconnect;//连接对象
     9     
    10     //操作数据库的方法
    11     
    12     //$sql代表需要执行的SQL语句
    13     //$type代表SQL语句的类型,1代表查询,2代表增删改
    14     //$shujukuming代表数据库的名称
    15     //如果是查询,返回二维数组
    16     //如果是增删改,返回true或false
    17     
    18     function Query($sql,$type=1,$shujukuming="newssystem")
    19     {
    20         //造连接对象
    21         $this->dbconnect = new MySQLi($this->fuwuqi,$this->yonghuming,$this->mima,$shujukuming);
    22         
    23         //判断是否出错
    24         if(!mysqli_connect_error())
    25         {
    26             //如果连接成功,执行SQL语句
    27             $result = $this->dbconnect->query($sql);
    28             
    29             //根据语句类型判断
    30             if($type==1)
    31             {
    32                //如果是查询语句,返回二维数组
    33                return $result->fetch_all();    
    34             }
    35             else
    36             {
    37                 //如果是其他语句,返回true或false
    38                return $result;    
    39             }
    40         
    41         }
    42         else
    43         {
    44             return"连接失败";
    45             
    46         }
    47         
    48         
    49     }
    50     
    51 }
    52 
    53 
    54 
    55 
    56 ?>

    【3】发布新闻页面---------fabuxinwen.php

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>无标题文档</title>
     6 </head>
     7 <body>
     8 <center><h1>发布新闻</h1> 
     9 
    10 <form action="fabuxinwenchuli.php" method="post">
    11 
    12 <div>标题:<input type="text" name="title" style="400px"/></div><br />
    13 
    14 <div>作者:<input type="text" name="Author" style="200px"/></div><br />
    15 
    16 <div>来源:<input type="text" name="source" style="400px"/></div><br />
    17 
    18 <div>内容:<textarea name="content" cols="50" rows="4" ></textarea></div><br />
    19 <br />
    20 
    21 <div><input type="submit" name="tijiao" value="提交"  />&nbsp;
    22 <a href="chakan.php"><input type="button" value="查看"/></a></div>
    23 </form>
    24 
    25 </center>
    26 </body>
    27 </html>

    【4】发布新闻的处理页面:----------fabuxinwenchuli.php

     1 <?php
     2  
     3 $title = $_POST["title"];
     4 $Author = $_POST["Author"];
     5 $source = $_POST["source"];
     6 $content  = $_POST["content"]; 
     7 $shijian= date("Y-m-d H:i:s");
     8 
     9 include("DBDA.class.php");
    10 
    11 $dx=new DBDA();
    12 
    13 $sql="insert into news values('','{$title}','{$Author}','{$source}','{$content}','{$shijian}')";
    14 
    15 
    16 $r = $dx->Query($sql,0);
    17 
    18 if($r)
    19 {
    20     header("location:fabuxinwen.php");
    21 }
    22 else
    23 {
    24     echo "修改失败!";
    25 }
    26 
    27 
    28 
    29 
    30 ?>

    【4】查看新闻页面----------chakan.php

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>查看新闻</title>
     6 <!--点击”查看“按钮,显示‘查看新闻’页面-->
     7 </head>
     8 <body>
     9 <table width="1000px" cellpadding="1" border="1"ellspacing="1">
    10 <tr>
    11 <td>编号</td>
    12 <td>标题</td>
    13 <td>作者</td>
    14 <td>来源</td>
    15 <td>内容</td>
    16 <td>时间</td>
    17 <td>修改</td>
    18 <td>删除</td>
    19 </tr>
    20 
    21 <?php
    22 include("DBDA.class.php");
    23 
    24 $dx=new DBDA();
    25 
    26 $sql="select * from news";
    27 $r = $dx->Query($sql,1);
    28 //$attr=$result->fetch_all();
    29 foreach($r as $v)
    30 {    
    31 echo "<tr>
    32 <td>{$v[0]}</td>
    33 <td>{$v[1]}</td>
    34 <td>{$v[2]}</td>
    35 <td>{$v[3]}</td>
    36 <td>{$v[4]}</td>
    37 <td>{$v[5]}</td>
    38 <td><a href='xiugai.php?newsid={$v[0]}'>修改</a></td>
    39 <td><a href='shanchu.php?newsid={$v[0]}'}>删除</a></td>
    40 </tr>";
    41 
    42 
    43 }
    44 
    45 ?>
    46 </table>
    47 </body>
    48 </html>

    【5】查看新闻的处理页面----------chakanchuli.php

     1 <?php
     2 
     3 $newsid = $_POST["newsid"];
     4 $title = $_POST["title"];
     5 $author = $_POST["author"];
     6 $source = $_POST["source"];
     7 $content = $_POST["content"];
     8 $time = $_POST["shijian"];
     9 
    10 include("DBDA.class.php");
    11 
    12 $dx=new DBDA();
    13 
    14 $sql = "update news set title = '{$title}',author = '{$author}',source = '{$source}',content = '{$content}',time= '{$time}' where newsid = '{$newsid}'";
    15 
    16 $result = $dx->query($sql);
    17 if($result)
    18 {
    19     header("location:chakan.php");
    20 }
    21 else
    22 {
    23     echo "修改失败";
    24 }
    25 ?>

    【6】删除处理页面--------shanchu.php

     1 <?php
     2 $newsid = $_GET["newsid"];
     3 var_dump($newsid);
     4 
     5 include("DBDA.class.php");
     6 
     7 $dx=new DBDA();
     8 
     9 $sql = "delete from news where newsid='{$newsid}'";
    10 $r = $dx->Query($sql,2);
    11 if($r)
    12 {
    13     header("location:chakan.php");
    14 }
    15 else
    16 {
    17     echo "删除失败!";
    18 }

    【7】修改页面---------xiugai.php

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 <html xmlns="http://www.w3.org/1999/xhtml">
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 <title>修改页面</title>
     6 </head>
     7 <body>
     8 <!--newsid--><center>
     9 <h1>修改页面</h1>
    10 <?php
    11 
    12 $newsid = $_GET["newsid"];
    13  
    14 include("DBDA.class.php");
    15 
    16 $dx=new DBDA();
    17 
    18 $sql="select * from news where newsid='{$newsid}'";
    19 $r = $dx->Query($sql);
    20 
    21 ?>
    22 
    23 
    24 <form action="xiugaichuli.php" method="post">
    25 <input type="hidden" name="newsid" value="<?php echo $r[0][0]; ?>"/>
    26 
    27 <div>标题:<input type="text" name="title" value="<?php echo $r[0][1];?>"/></div>
    28 <div>作者:<input type="text" name="author" value="<?php echo $r[0][2];?>"/></div>
    29 <div>来源:<input type="text" name="source" value="<?php echo $r[0][3];?>"/></div>
    30 <div>内容:<textarea name="content" cols="100" rows="10"><?php echo $r[0][4];?></textarea></div>
    31 
    32 <div><input type="submit" value="修改"/></div>
    33 <input type="hidden" name="newsid" value="<?php echo $newsid; ?>" />
    34 
    35 </form>
    36 <!--<a href="chakan.php"><input type="button" value="查看"></a>-->
    37 </center>
    38 </body>
    39 </html>

    【8】修改的处理页面---------xiugaichuli.php

     1 <?php
     2  
     3 $newsid = $_POST["newsid"];
     4 $title = $_POST["title"];
     5 $Author = $_POST["author"];
     6 $source = $_POST["source"];
     7 $content  = $_POST["content"]; 
     8 
     9 
    10 include("DBDA.class.php");
    11 
    12 $dx=new DBDA();
    13 
    14 $sql="update news set title='{$title}',author = '{$Author}',source ='{$source}',content='{$content}' where newsid = '{$newsid}'";  
    15 echo $sql;
    16 $r = $dx->Query($sql,0);
    17 var_dump($r);
    18 if($r)
    19 {
    20     header("location:chakan.php");
    21 }
    22 else
    23 {
    24     echo "修改失败!";
    25 }
    26 
    27 
    28 
    29 
    30 ?>

  • 相关阅读:
    markdown 常用语法 (在macdown内使用正常)
    C# MessageBox常用用法
    C# 正则表达式匹配汉字
    C# 可视化读取文件、文件夹
    Xcode UUID查询
    Xcode 常用快捷键
    iOS __weak __strong WeakSelf StrongSelf
    iOS 图形图像动画 Core Animation
    iOS Runtime学习笔记
    iOS NSDate本地化
  • 原文地址:https://www.cnblogs.com/yuyu1993/p/5598691.html
Copyright © 2011-2022 走看看