PHP開發(fā)新聞管理系統(tǒng)之修改功能的實現(上)
上節(jié)講到我們數據庫的信息已經展示出來,大家有沒有注意到修改和刪除倆個連接,我寫了一個輸出 id 的語句
<a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a>
<a href="delnew.php?id=<?php echo $row['id'];?>">刪除</a>
修改,我們肯定要獲取 id , 根據 id 從數據查詢,然后修改該id的其他的字段的內容,我們來看以下修改的流程圖
點擊修改,會帶著一個 id 傳到modifynew.php 這個文件中,
這個頁面就是一個修改的頁面,效果如下:
在這個頁面中,我們要根據 剛才傳過來的 id 進行在數據庫查詢,然后把標題內容顯示出來
首先鏈接數據庫:
header("Content-type: text/html; charset=utf-8");//設置編碼
$con =@mysql_connect("localhost","root","root") or die("數據庫連接失敗");
mysql_select_db('news') or die("指定的數據庫不能打開");
mysql_query("set names utf8");//設置數據庫的字符集
然后獲取 id
表單上帶的 id 我們使用 get 方式獲取
$id=$_GET['id'];
我們根據 id 在數據庫進行查詢
$sql="select * from new where id=$id";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
查詢到信息之后,我們要在頁面上把信息展示出來
html 頁面的代碼如下:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{margin:0px;padding:0px;} body{background:#ccc;} .add{width:450px;height:280px;background:#eee;float:left;} .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;} form{margin-left:10px;padding-top:30px;} .sub{width:100px;height:40px;border:1px solid #ccc;} .sub:hover{background:#f90} </style> </head> <body> <div class="add"> <div class="cont"> <form method="post" action="modify.php?id=<?php echo $id;?>"> 標題:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br> 內容:<textarea cols="50" rows="5" name="content"><?php echo $row['content']?></textarea></br></br> <input type="submit" value="修改" class="sub"> </form> </div> </div> </body> </html>
這樣我們從數據庫查詢到的信息就展示出來了
完整源碼如下:
<?php header("Content-type: text/html; charset=utf-8");//設置編碼 $con =@mysql_connect("localhost","root","root") or die("數據庫連接失敗"); mysql_select_db('news') or die("指定的數據庫不能打開"); mysql_query("set names utf8");//設置數據庫的字符集 $id=$_GET['id']; $sql="select * from new where id=$id"; $res = mysql_query($sql); $row = mysql_fetch_array($res); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{margin:0px;padding:0px;} body{background:#ccc;} .add{width:450px;height:280px;background:#eee;float:left;} .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;} form{margin-left:10px;padding-top:30px;} .sub{width:100px;height:40px;border:1px solid #ccc;} .sub:hover{background:#f90} </style> </head> <body> <div class="add"> <div class="cont"> <form method="post" action="modify.php?id=<?php echo $id;?>"> 標題:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br> 內容:<textarea cols="50" rows="5" name="content"><?php echo $row['content']?></textarea></br></br> <input type="submit" value="修改" class="sub"> </form> </div> </div> </body> </html>