php增刪改查,分頁
阿新 • • 發佈:2019-01-05
首頁:
<?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'); }