php数据访问之增删改查操作

yipeiwu_com5年前PHP代码库

增删改查操作小练习,大家练练手吧

一、查看新闻页面-----主页面

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>查看新闻</title>
</head>
 
<body>
<h1>查看新闻</h1>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
  <td>id</td>
  <td>title</td>
  <td>author</td>
  <td>source</td>
   <td>content</td>
  <td>date</td>
  <td>update</td>
  <td>delete</td>
</tr>
<?php
$db=new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败!");
$sql="select * from news";
$result=$db->query($sql);
$arr=$result->fetch_all();
foreach ($arr as $v)
{
  echo "<tr>
  <td>{$v[0]}</td>
  <td>{$v[1]}</td>
  <td>{$v[2]}</td>
  <td>{$v[3]}</td>
  <td>{$v[4]}</td>
  <td>{$v[5]}</td>
  <td><a href='Update.php?newsid={$v[0]}'>update</a></td>
  <td><a href='Delete.php?newsid={$v[0]}'>delete</a></td>
   
  </tr>";
  }
 
?>
</table>
<br>
<br />
<div class="xw"><a href="xinwen.php">发布新闻</a></div>
 
</body>
</html>

  

二、发布新闻页面-----添加内容

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>发布新闻</title>
<style>
.xw
{
   
  margin-top:10px;
  margin-left:400px;
  border:thick; 
  }
.a
{
  float:left;
   
  }
 
</style>
 
</head>
 
<body>
<h1><center>发布新闻</center></h1>
 
<form action="AddChu.php" method="post">
<div class="xw">标题:<input type="text" name="title" style="width:400px"></div>
<div class="xw">作者:<input type="text" name="author"></div>
<div class="xw">来源:<input type="text" name="source"></div>
<div class="xw">内容:
<textarea rows="10" cols="80" name="content"></textarea></div>
 
<div class="a"><input type="submit" value="提交" style="margin-left:600px;"></div>
<div class="a"><a href="ChaKan.php"><input type="button" value="查看" style="margin-left:6px;"></a></div>
 
</form>
 
 
</body>
</html>

  

提交内容后的处理:

<?php
$newsid=$_POST["newsid"];
$title=$_POST["title"];
$author=$_POST["author"];
$source=$_POST["source"];
$content=$_POST["content"];
$time=date("Y-m-d",time());
 
$db=new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("联系失败!");
$sql="insert into news values('{$newsid}','{$title}','{$author}','{$source}','{$content}','{$time}')";
$result=$db->query($sql);
if($result)
{
  header ("location:xinwen.php");
  }
else
{
  echo "添加新闻失败!";
  }

三、删除内容处理

<?php
$newsid=$_GET["newsid"];
$db=new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("连接失败!");
$sql="delete from news where newsid='{$newsid}'";
$result=$db->query($sql);
if($result)
{
  header ("location:ChaKan.php");
  }
else
{
  echo "删除数据失败";
  }
?>
  

四、修改新闻页面----修改新闻内容后提交查看

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>修改新闻</title>
<style>
.xw
{
   
  margin-top:10px;
  margin-left:400px;
  border:thick; 
  }
   
.a
{
  float:left;
   
  }
 
</style>
</head>
 
<body>
<h1><center>修改新闻</center></h1>
<?php
$newsid = $_GET["newsid"];
$db = new MySQLi("localhost","root","","mydb");
$sinfo = "select * from news where newsid='{$newsid}'";
$r = $db->query($sinfo);
$arr = $r->fetch_row(); //这个人的所有信息
?>
 
<form action="UpdateChu.php" method="post">
<div class="xw"><input type="hidden" name="newsid" value="<?php echo $arr[0] ?>"></div>
<div class="xw">标题:<input type="text" name="title" style="width:400px" value="<?php echo $arr[1] ?>"></div>
<div class="xw">作者:<input type="text" name="author" value="<?php echo $arr[2] ?>"></div>
<div class="xw">来源:<input type="text" name="source" value="<?php echo $arr[3] ?>"></div>
<div class="xw">内容:
<textarea rows="10" cols="80" name="content"><?php echo $arr[4] ?></textarea></div>
 
<div class="a"><input type="submit" value="修改" style="margin-left:600px;"></div>
<div class="a"><a href="ChaKan.php"><input type="button" value="查看" style="margin-left:6px;"></a></div>
 
</form>
 
</body>
</html>  

  

提交修改内容后进行处理:

<?php
$newsid=$_POST["newsid"];
$title=$_POST["title"];
$author=$_POST["author"];
$source=$_POST["source"];
$content=$_POST["content"];
$time=date("Y-m-d",time());

$db=new MySQLi("localhost","root","","mydb");
!mysqli_connect_error() or die("联系失败!");
$sql="update news set title='{$title}',author='{$author}',source='{$source}',content='{$content}',time='{$time}' where newsid='{$newsid}'";
$result=$db->query($sql);
if($result)
{
  header ("location:Update.php");
  }
else
{
  echo "修改数据失败!";
  }

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

相关文章

PHP定时自动生成静态HTML的实现代码

但定时生成就受到了一些局限性,大家如果有独立服务器的能在服务器上设置计划任务,但如果是使用虚拟主机的可就不好办了.虽然方法非常多.但使用起来简便容易的,我觉得还是先判断已生成的首页文件的...

PHP中使用asort进行中文排序失效的问题处理

PHP中有非常方便的对数组进行重新排序的方法——asort,关于asort的使用方法可以看 这里 。但是asort对含有中文key的数组进行排序时,有时候并不是按照字母顺序。这主要是编码...

PHP常见过waf webshell以及最简单的检测方法

PHP常见过waf webshell以及最简单的检测方法

前言 之前在Webshell查杀的新思路中留了一个坑 ️,当时没有找到具体找到全部变量的方法,后来通过学习找到了个打印全部量的方法,并再次学习了下PHP webshell绕...

在Windows系统下使用PHP生成Word文档的教程

在Windows系统下使用PHP生成Word文档的教程

准备工作 首先,请确保在你的Windows系统中已经安装并配置好了一个典型的WAMP环境。由于Interop纯粹是一个Windows的特性,我们将在Windows平台下搭建Apache和...

深入PHP获取随机数字和字母的方法详解

第一种方法复制代码 代码如下:<?php  $FileID=date("Ymd-His") . '-' . rand(100,999);  //$FileID为 &nbs...