php的增删改

来源:互联网 发布:大数据采集与预处理 编辑:程序博客网 时间:2024/05/19 02:05

数据库
-- phpMyAdmin SQL Dump
-- version phpStudy 2014
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2016 年 10 月 13 日 17:21
-- 服务器版本: 5.5.47
-- PHP 版本: 5.3.29


SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";




/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;


--
-- 数据库: `test`
--


-- --------------------------------------------------------


--
-- 表的结构 `new`
--


CREATE TABLE IF NOT EXISTS `new` (
  `id` int(4) NOT NULL AUTO_INCREMENT,
  `title` varchar(32) NOT NULL,
  `content` text NOT NULL,
  `time` date NOT NULL,
  `author` varchar(32) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=103338 ;


--
-- 转存表中的数据 `new`
--


INSERT INTO `new` (`id`, `title`, `content`, `time`, `author`) VALUES
(41, '  我是', 'phiopip', '0000-00-00', '订单'),
(42, 'ddyyttfy', 'ggouiuio', '0000-00-00', ''),
(43, 'iygiyi', 'iyyuiuyi', '0000-00-00', ''),
(44, '123', '11', '0000-00-00', 'rwerr'),
(55, 'qqq', 'rartaser', '0000-00-00', 'eresrsr'),
(100, 'qqq', 'rartaser', '0000-00-00', 'eresrsr'),
(105, 'qqq', 'rartaser', '0000-00-00', 'eresrsr'),
(1111, '1111111', '1111111', '0000-00-00', '111111111'),
(103335, 'qqq', 'rartaser', '0000-00-00', 'eresrsr'),
(103336, '333333', '', '0000-00-00', ''),
(103337, '333333', '我是测试内容', '1970-05-24', '张三');


/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;



action.php //用来处理增删改的程序
<?php
require("config.php");
switch($_GET["action"])
{
case "add" :


$id=$_POST["id"];
$title=$_POST["title"];
$content=$_POST["content"];
$time=time();
$author=$_POST["author"];




$sql="insert into new values($id ,$title,$content,$time,$author)";
mysql_query($sql,$connect);


$a=mysql_insert_id($connect);
if($a>0)
{
echo "新闻添加成功";




}
else
{
echo "新闻添加失败";


}


case "del" :
$id=  $_GET['id'];




$sql="delete from new where id=$id";


mysql_query($sql,$connect);
case "update" :
//获取edit.php传过来的值


$id=$_POST['id'];


$title=$_POST['title'];


//echo $title;
$time=$_POST['time'];
$content=$_POST['content'];
$author=$_POST['author'];


$sql="update new  set title=' {$title}',time='{$time}' , content='{$content}' ,author='{$author}' where id='{$id}'";


//
mysql_query($sql,$connect);
////
header("locatioa:index.php");


}




 ?>




add.php



<?php include("menu.php") ?>
发布新闻
<form action="action.php?action=add" method="post">


<input name="id" type="text" />
<input name="title" type="text" />
<input name="content" type="text" />
<input name="time" type="text" />
<input name="author" type="text" />


<input  type="submit" value="添加" />
<input  type="reset" value="重置"/>


</form>



config.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<?php




define(DBHost, "localhost");                    //自定义常量DBhost(服务器名)
define(DBUser, "root");                         //自定义常量DBUser(登录服务器的用户名)
define(DBPassword, "root");
 
  
  $connect=mysql_connect(DBHost,DBUser,DBPassword) or die("无法连接数据库服务器!");//连接服务器
  mysql_select_db(test,$connect);
   mysql_query(" set names 'gb2312'");
     
?>
<body>
</body>
</html>




edit.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>


<body>
<?php






require("config.php");
   $id= $_GET[id];
   $sql="select * from new where id= $id";
   echo $sql;
$result=   mysql_query($sql,$connect);
if($result && mysql_num_rows($result)>0)
{
$row=mysql_fetch_assoc($result);
}








 ?>
  <form action="action.php?action=update" method="post">


<input name="id" type="text" value="<?php echo $row['id']?>" />
<input name="title" type="text"   value="<?php echo $row['title'];?>"    />
<input name="content" type="text"   value="<?php   echo $row['content'];?>" />
<input name="time" type="text"  value="<?php   echo $row['time'];?>"   />
<input name="author" type="text"  value="<?php echo $row['author'];?>"/>


<input  type="submit" value="编辑" />
<input  type="reset" value="重置"/>


</form>


</body>
</html>

index.php
<html>
<head>


<script type="text/javascript">
function del(id)
{
if(confirm("确定要删除吗?"))
{
window.location="action.php?action=del&id="+id;


}


//alert(id);


}


</script>






</head>
<body>
<?php


require("config.php");


$sql1="select * from new";
$result= mysql_query($sql1,$connect);
      echo "<table border=1>"; 
while( $row = mysql_fetch_array($result) )
/*逐行获取结果集中的记录,得到数组row */
{  
/*数组row的下标对应着数据库中的字段值 */
$id = $row['id']; 
$title = $row['title']; 
$content = $row['content']; 
$time = $row['time'];
$author = $row['author'];




echo "<tr>"; 
echo "<td>$id</td>"; 
echo "<td>$title</td>"; 
echo "<td>$content</td>"; 




echo "<td>$time</td>"; 
echo "<td>$author</td>"; 
echo "<td> <a href='edit.php?id=$id'>修改 </a>   
<a href='javascript:del($id)'>删除 </a>


 </td>"; 


echo "</tr>"; 

echo "<table />"; 






?>


</body>


menu.php

</html>
<h2>新闻管理系统</h2>
<a href="index.php">浏览新闻</a>
<a href="add.php">发布新闻</a>
0 0
原创粉丝点击