php的增删改


数据库
-- 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个简单的示例,展示如何在 PHP 中添加增删功能的代码: 添加数据: ```php <?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "your_database_name"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取表单数据 $name = $_POST['name']; $email = $_POST['email']; // 添加数据到数据库 $sql = "INSERT INTO your_table_name (name, email) VALUES ('$name', '$email')"; if ($conn->query($sql) === TRUE) { echo "数据添加成功"; } else { echo "数据添加失败: " . $conn->error; } // 关闭连接 $conn->close(); ?> ``` 删除数据: ```php <?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "your_database_name"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取要删除的数据的 ID $id = $_GET['id']; // 从数据库删除数据 $sql = "DELETE FROM your_table_name WHERE id = $id"; if ($conn->query($sql) === TRUE) { echo "数据删除成功"; } else { echo "数据删除失败: " . $conn->error; } // 关闭连接 $conn->close(); ?> ``` 更新数据: ```php <?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = ""; $dbname = "your_database_name"; $conn = new mysqli($servername, $username, $password, $dbname); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 获取表单数据 $id = $_POST['id']; $name = $_POST['name']; $email = $_POST['email']; // 更新数据库中的数据 $sql = "UPDATE your_table_name SET name='$name', email='$email' WHERE id=$id"; if ($conn->query($sql) === TRUE) { echo "数据更新成功"; } else { echo "数据更新失败: " . $conn->error; } // 关闭连接 $conn->close(); ?> ``` 请注意,上述示例中的 "your_database_name" 和 "your_table_name" 是需要根据你的实际情况进行替换的。此外,为了安全起见,建议在实际应用中使用预处理语句来防止 SQL 注入攻击。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值