php增删改查,分页

时间:2022-10-20 23:32:57

首页:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysqli_connect_error() or die('连接失败');
$db->query('set names utf8');

$page = 1;//页码
$pagesize = 2;//每页显示多少页

//接收页码
if(!empty($_GET['page'])){
	$page = $_GET['page'];
}


//计算页码
//查出总条数
$sql = "select count(*) from new-news ";
$res = $db->query($sql);
$num = $res->fetch_row();
//计算总条数除以每页的页数,向上取整
$pageNum = ceil($num[0]/$pagesize);


//组织limit条件,分页公式
$limit = " limit ".$pagesize*($page-1).",$pagesize";


//查数据

$sql = "select * from new-news ".$limit;
$res = $db->query($sql);

$arr = array();//定义数组
while($row = $res->fetch_assoc()){//定义一行的信息,关联数组
	$arr[] = $row;//追加
}


?>







<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>首页</title>
</head>

<body>
<a href="edit.php">发布信息</a>
<table width="80%" border="1" cellpadding="0" cellspacing="0">
	<tr>
		<th>id</th>
		<th>title</th>
		<th>author</th>
		<th>sourse</th>
		<th>content</th>
		<th>date</th>
		<th>update</th>
		<th>detele</th>
	</tr>
	
	<?php foreach($arr as $v){ ?>
		<tr>
			<?php foreach($v as $k => $vv){ ?>
				if($k == 'content') continue;
				 echo "<td>$vv</td>";
				
			<?php } ?>
			<td><a href="edit.php?newsid=<?php echo $v['newsid']; ?>">update</a></td>
			<td><a href="chuli.php?type=del&newsid=<?php echo $v['newsid']; ?>">delete</a></td>
		</tr>
	
	
	<?php } ?>
	
	<button>上一页</button>
	<?php for($i = 1;$i<$pageNum;$i++){
			echo "<a href=\"index.php?$page=$i\"<button>$i</button></a> ";
		}
	?>
	<button>下一页</button>
</table>
</body>
</html>

  添加处理页:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysqli_connect_error() or die('连接失败');
$db->query('set names utf8');


$id = "";
$attr = array();
if(!empty($_GET['newsid'])){
	$id = $_GET['newsid'];
	
	//查数据
	$sql = "select * from new-news where newsid = $id";
	$res = $db->query($sql);
	$attr = $res->fetch_row();
	
}
?>




<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>添加处理页</title>
</head>

<body>
<form action="chuli.php?type=<?php echo $id == '' ? 'add' : 'update' ?>" method="post">
	标题: <input type="text" name="title" value="<?php echo $id ? $attr[1] : ''; ?>"> <br>
	作者: <input type="text" name="author" value="<?php echo $id ? $attr[2] : ''; ?>"> <br>
	来源: <input type="text" name="source" value="<?php echo $id ? $attr[3] : ''; ?>"> <br>
	内容: <textarea name="content" id="" cols="30" rows="10">
			<?php echo $id ? $attr[4] : ''; ?>
	 </textarea> <br>
	<button>
		<?php echo $id ? '修改信息' : '发布信息'; ?>
	</button>
	<a href="index.php"><button>查看</button></a>
</form>
</body>
</html>

  处理页:

<?php
//连接数据库
$db = new MySQLi('localhost','root','','z_1032');
!mysqli_connect_error() or die('连接失败');
$db->query('set names utf8');



$type = $_REQUEST['type'];
switch('$type'){
		case 'update':
		$id = $_POST['newsid'];
		$title = $_POST['title'];
		$author = $_POST['author'];
		$source = $_POST['source'];
		$content = $_POST['content'];
		$time = data('Y-m-d H:i:s');
		
		$sql = "update new_news set title = $title,set author = $author,set source = $source,set content = $content where newsid = $id";
		$res = $db->query($sql);
		if($res){
			echo "修改成功";
			header("location:index.php");
		}else{
			echo "修改失败";
			header('refresh:3,url=index.php');
		}
		break;
	case 'add':
		//接收值
		$title = $_POST['title'];
		$author = $_POST['author'];
		$source = $_POST['source'];
		$content = $_POST['content'];
		$time = data('Y-m-d H:i:s');


		$sql = "insert into new_news(title,author,source,content,time) values('$title','$author','$source','$content','$time')";
		
		break;
	case 'del':
		$id = $_GET['newsid'];
		$sql = "delete from new_news where newsid = $id";
		break;
		
	
}
$res = $db->query($sql);
if($res){
	header("location:index.php");
}else{
	echo error;
	header('refresh:3,url=index.php');
}