標籤:des style blog http color os io 資料
留言板或者說一個小的部落格系統有如下功能,編寫標題內容並用mySQL儲存,修改,刪除。
1 <?php2 @mysql_connect("localhost:3306", "root", "") or die("mysql串連失敗");3 @mysql_select_db("php100") or die("db串連失敗");4 //mysql_set_charset("gbk"); 5.2.3以上可以這樣寫,糾正函數編碼5 mysql_query("set names ‘gb2312_chinese_ci‘"); //6 mysql_query("SET NAMES ‘UTF8‘"); //以此來保證網頁傳送中文漢字進入資料庫時不會變成亂碼7 ?>conn.php
1 <a href=‘add.php‘>新增內容</a> 2 <hr> 3 <?php 4 header("Content-type: text/html; charset=utf-8");//保證正常顯示中文 5 include_once ("conn.php"); 6 7 $sql = "select * from `news` order by id desc"; 8 $query = mysql_query($sql); 9 10 while( $rs = mysql_fetch_array($query)) {//執行一次向下讀取一條資料11 12 ?>13 <h2>標題:<?php echo $rs["title"] ?> |14 <a href="edit.php?ed=<?php echo $rs[‘id‘]?>">編輯</a> | 15 <a href="del.php?del=<?php echo $rs[‘id‘]?>">刪除</a> |</h2>16 <li>時間:<?php echo $rs["dates"] ?></li>17 <p><?php echo $rs["contents"] ?></p>18 <hr>19 <?php//典型的混編,簡直醉了20 }21 ?>index.php
1 <?php//新增內容頁面 2 header("Content-type: text/html; charset=utf-8"); 3 include ("conn.php");//引入串連資料庫 4 5 if(!empty($_POST[‘sub‘])){ 6 $title = $_POST[‘title‘]; 7 $con = $_POST[‘con‘]; 8 /*$sql = "CREATE TABLE news //這個是建立表的語句 9 (10 id INT(5) NOT NULL AUTO_INCREMENT, 11 PRIMARY KEY(id),12 title VARCHAR(50),13 dates DATE,14 contents TEXT15 )";*/16 $sql="insert into `news` (`id`,`title`,`dates`, `contents`) value (null, ‘$title‘,now(),‘$con‘)";17 18 echo $sql."<br/>";19 mysql_query($sql);20 echo "insert is ok";21 }22 23 24 ?>25 26 <form action="add.php" method="post">27 標題<input type="text" name="title"><br>28 內容<textarea rows="5" cols="50" name="con"></textarea><br>29 <input type="submit" name="sub" value="發表">30 </form>add.php
1 <?php 2 3 include ("conn.php"); 4 header("Content-type: text/html; charset=utf-8"); 5 6 if(!empty($_GET[‘del‘])) {//在index頁面中,串連後面接著的?del=‘$id‘可以用get方法獲得,以此來簡單的傳遞資訊 7 $d = $_GET[‘del‘]; 8 $sql = "delete from `news` where `id` = ‘$d‘"; 9 mysql_query($sql);10 echo "刪除成功";11 }12 ?>del.php
1 <?php 2 include_once("conn.php"); 3 header("Content-type: text/html; charset=utf-8"); 4 if(!empty($_GET[‘ed‘])){//同樣用連結的尾碼傳遞id 5 $ed = $_GET[‘ed‘]; 6 $sql = "select * from `news` where `id`=‘$ed‘"; 7 $query = mysql_query($sql); 8 $rs = mysql_fetch_array($query); 9 ?>10 11 <h2>標題:<?php echo $rs[‘title‘]?></h2>12 <li>時間:<?php echo $rs[‘dates‘]?></li>13 <form method="POST" action="edit.php">14 <input type="hidden" name="hid" value="<?php echo $ed?>"/>//隱藏的id來傳遞,用post方法,這種寫法也學會,並不會在頁面顯示15 內容:<textarea name="con" rows="5" cols="40"><?php echo $rs[‘contents‘]?></textarea>16 <br><br>17 <input type="submit" name="submit" value="修改"/>18 </form>19 20 <?php21 }22 23 if(!empty($_POST[‘submit‘])) {24 $content = $_POST[‘con‘];25 $hid = $_POST[‘hid‘];26 $sql = "update `news` set `contents`=‘$content‘ where `id`=‘$hid‘";27 mysql_query($sql);28 echo "<script>alert(‘更新成功‘);location.href=‘index.php‘</script>";29 }30 31 ?>edit.php