PHP開發(fā)新聞管理系統(tǒng)之修改功能的實(shí)現(xiàn)(上)
上節(jié)講到我們數(shù)據(jù)庫(kù)的信息已經(jīng)展示出來(lái),大家有沒(méi)有注意到修改和刪除倆個(gè)連接,我寫了一個(gè)輸出 id 的語(yǔ)句
<a href="modifynew.php?id=<?php echo $row['id'];?>">修改</a>
<a href="delnew.php?id=<?php echo $row['id'];?>">刪除</a>
修改,我們肯定要獲取 id ,? 根據(jù)? id 從數(shù)據(jù)查詢,然后修改該id的其他的字段的內(nèi)容,我們來(lái)看以下修改的流程圖
點(diǎn)擊修改,會(huì)帶著一個(gè) id 傳到modifynew.php 這個(gè)文件中,
這個(gè)頁(yè)面就是一個(gè)修改的頁(yè)面,效果如下:
在這個(gè)頁(yè)面中,我們要根據(jù) 剛才傳過(guò)來(lái)的 id 進(jìn)行在數(shù)據(jù)庫(kù)查詢,然后把標(biāo)題內(nèi)容顯示出來(lái)
首先鏈接數(shù)據(jù)庫(kù):
????header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
?? ?$con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗");
?? ?mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開");
?? ?mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集
然后獲取? id?
表單上帶的 id? 我們使用 get 方式獲取
$id=$_GET['id'];
我們根據(jù) id 在數(shù)據(jù)庫(kù)進(jìn)行查詢
????$sql="select * from new where id=$id";
?? ?$res = mysql_query($sql);
?? ?$row = mysql_fetch_array($res);
查詢到信息之后,我們要在頁(yè)面上把信息展示出來(lái)
html 頁(yè)面的代碼如下:
<!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;?>"> 標(biāo)題:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br> 內(nèi)容:<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>
這樣我們從數(shù)據(jù)庫(kù)查詢到的信息就展示出來(lái)了
完整源碼如下:
<?php header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫(kù)連接失敗"); mysql_select_db('news') or die("指定的數(shù)據(jù)庫(kù)不能打開"); mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫(kù)的字符集 $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;?>"> 標(biāo)題:<input type="text" name="title" value="<?php echo $row['title']?>"></br></br> 內(nèi)容:<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>