zoukankan      html  css  js  c++  java
  • 数据的增删改练习——新闻消息的发布、更改、删除

    第一:先构建“进入数据库,并执行语句方法”的类

    <?php
    class CHAXUN
    {
    	public $host="localhost";
    	public $root="root";
    	public $pwd="";
    	
    	function Query($sql,$type=0,$ku="newssysstem")
    	{
    		$db=new MySQLi($this->host,$this->root,$this->pwd,$ku);
    		!mysqli_connect_error() or die("连接失败!");
    		$result=$db->query($sql);
    		if($type==0)
    		{
    			return $result->fetch_all();
    		}
    		else
    		{
    			return $result;
    		}
    	}
    }
    

      

    第二:查看并显示新闻

    <title>查看新闻</title>
    </head>
    
    <body>
    <div align="center">
    
    <h1>查看新闻</h1>
    <table border="1" width="1000" cellpadding="0" cellspacing="0">
      <tr>
         <td>newsid</td>
         <td>title</td>
         <td>author</td>
         <td>source</td>
         <td>date</td>
         <td>update</td>
         <td>delete</td>
      </tr>
      <?php
         include("CHAXUN.class.php");
         $db=new CHAXUN();
    	 $sql="select * from news";
    	 $attr=$db->Query($sql);
    	 //var_dump($attr);
    	 foreach($attr as $v)
    	 {
    		 echo "<tr>
    		           <td>{$v[0]}</td>
    				   <td>{$v[1]}</td>
    				   <td>{$v[2]}</td>
    				   <td>{$v[3]}</td>
    				   <td>{$v[5]}</td>
    				   <td>
    				   <a href='updatexw.php?newsid={$v[0]}'}>update</a>
    				   </td>
    				   <td>
    				   <a href='deletexw.php?newsid={$v[0]}'>delete</a>
    				   </td>
    		       </tr>";
    	 }
      ?>
    </table>
    <a target="_blank" href="fabuxinwen.php">发布新闻</a>
    
    
    </div>
    </body>
    </html>
    

      

    第三步:发布新闻 

         1.页面显示代码

            

    <title>发布新闻</title>
    </head>
    
    <body>
    <div align="center">
    
    
    <div align="left">
    <h1>发布新闻</h1>
    <form action="fbxwchuli.php" method="post">
    标题:<input type="text" name="title" style="250px"/><br />
    <br />
    
    作者:<input type="text" name="author" style="150px"/><br />
    <br />
    
    来源:<input type="text" name="source" style="150px"/><br />
    <br />
    
    内容:<textarea name="content" rows="10" style="350px"></textarea><br />
    <br />
          
    <input type="submit" name="tijiao" value="提交"/>
    <a href="chakan.php" target="_blank"><input type="button" value="查看"/></a>
    </form>
    </div>
    
    
    </div>
    </body>
    </html>
    

      

          2.发布新闻后台运行

    <?php
    
    $title=$_POST["title"];
    $author=$_POST["author"];
    $source=$_POST["source"];
    $content=$_POST["content"];
    
    date_default_timezone_set("Etc/GMT-8");
    $time=date("Y-m-d H:i:s",time());
    
    include("CHAXUN.class.php");
    $db=new CHAXUN();
    
    $sql="insert into news (title,author,source,content,time)
     values('{$title}','{$author}','{$source}','{$content}','{$time}')";
    $r=$db->Query($sql,1);
    if($r)
    {
    	header("location:fabuxinwen.php");
    }
    else
    {
    	echo "发布失败!";
    }
    

      

    第四步:更改新闻内容

             4.1页面显示代码:

    <title>更改新闻</title>
    </head>
    
    <body>
    <div align="left" >
    <h1>更改新闻</h1>
    
    <?php
    $newsid=$_GET["newsid"];
    
    include("CHAXUN.class.php");
    $db=new CHAXUN();
    $sql="select * from news where newsid={$newsid}";
    $attr=$db->Query($sql);
    
    ?>
    
    <form action="updatexwchuli.php" method="post">
    <input type="hidden" name="newsid" value="<?php echo "{$attr[0][0]}";?>"/>
    标题:<input style="250px" type="text" name="title" value="<?php echo "{$attr[0][1]}";?>"/><br />
    <br />
    
    作者:<input style="150px" type="text" name="author" value="<?php echo "{$attr[0][2]}";?>"/><br />
    <br />
    
    来源:<input style="150px" type="text" name="source" value="<?php echo "{$attr[0][3]}";?>"/><br />
    <br />
    
    内容:<textarea name="content" rows="10" style="350px"><?php echo "{$attr[0][4]}";?></textarea><br />
    <br />
          
    <input type="submit" name="gengai" value="更改"/>
    </form>
    </div>
    </body>
    </html>
    

      

      4.2更改新闻内容后端处理代码

    <?php
    $newsid=$_POST["newsid"];
    $title=$_POST["title"];
    $author=$_POST["author"];
    $source=$_POST["source"];
    $content=$_POST["content"];
    
    date_default_timezone_set("Etc/GMT-8");
    $time=date("Y-m-d H:i:s",time());
    
    include("CHAXUN.class.php");
    $db=new CHAXUN();
    
    $sql="update news set title='{$title}',author='{$author}',source='{$source}',content='{$content}',time='{$time}' where newsid={$newsid}";
    
    $r=$db->Query($sql,1);
    if($r)
    {
    	header("location:chakan.php");
    }
    else
    {
    	echo "更改失败!";
    }
    

    第五步:删除新闻(只有后台运行代码)

    <?php
    $newsid=$_GET["newsid"];
    
    include("CHAXUN.class.php");
    $db=new CHAXUN();
    $Sql="delete from news where newsid={$newsid}";
    $r=$db->Query($Sql,1);
    
    if($r)
    {
    	header("location:chakan.php");
    }
    else
    {
    	echo "删除失败!!";
    }
    

      

  • 相关阅读:
    jQuery.hover() 函数详解
    深入了解css的行高Line Height属性
    yii2 restfulapi QueryParamAuth验证
    yii2 restfulapi 的配置和访问
    yii2 urlmanager的配置
    xubuntu install nodejs
    使用Putty连接VirtualBox的Ubuntu
    mvc与mvvm
    对二叉树进行广度优先遍历
    JavaScript 中的 FileReader
  • 原文地址:https://www.cnblogs.com/zst062102/p/5471616.html
Copyright © 2011-2022 走看看