PHP Mysql如何實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查?

發(fā)布時(shí)間:2024-03-16
php mysql實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查的方法:1、創(chuàng)建入口文件【index.html】連接數(shù)據(jù)庫(kù)、查詢數(shù)據(jù);2、點(diǎn)擊增加按鈕,通過【addnews.html】添加數(shù)據(jù);3、點(diǎn)擊刪除按鈕,通過服務(wù)端文件【action-del.php】進(jìn)行刪除。
php mysql實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查的方法:
1. 查詢數(shù)據(jù)庫(kù)
1.1. 創(chuàng)建文件dbconfig.php,保存常量
<?php define("host","localhost"); define("user","root"); define("pass","");define("dbname","news");1.2. 創(chuàng)建入口文件index.html(連接數(shù)據(jù)庫(kù)、查詢數(shù)據(jù))
<!doctype html><html><head><meta charset="utf-8"><title>新聞后臺(tái)管理系統(tǒng)</title></head><style type="text/css">.wrapper {width: 1000px;margin: 20px auto;}h2 {text-align: center;}.add {margin-bottom: 20px;}.add a {text-decoration: none;color: #fff;background-color: green;padding: 6px;border-radius: 5px;}td {text-align: center;}</style><body><div><h2>新聞后臺(tái)管理系統(tǒng)</h2><div><a href="addnews.html">增加新聞</a></div><table width="960" border="1"><tr><th>id</th><th>標(biāo)題</th><th>關(guān)鍵字</th><th>作者</th><th>發(fā)布時(shí)間</th><th>內(nèi)容</th><th>操作</th></tr><?php // 1.導(dǎo)入配置文件 require "dbconfig.php"; // 2. 連接mysql $link = @mysql_connect(host,user,pass) or die("提示:數(shù)據(jù)庫(kù)連接失敗!"); // 選擇數(shù)據(jù)庫(kù) mysql_select_db(dbname,$link); // 編碼設(shè)置 mysql_set_charset('utf8',$link);// 3. 從dbname中查詢到news數(shù)據(jù)庫(kù),返回?cái)?shù)據(jù)庫(kù)結(jié)果集,并按照addtime降序排列 $sql = 'select * from news order by id asc'; // 結(jié)果集 $result = mysql_query($sql,$link); // var_dump($result);die;// 解析結(jié)果集,$row為新聞所有數(shù)據(jù),$newsnum為新聞數(shù)目$newsnum=mysql_num_rows($result); for($i=0; $i<$newsnum; $i ){$row = mysql_fetch_assoc($result);echo "<tr>";echo "<td>{$row['id']}</td>";echo "<td>{$row['title']}</td>";echo "<td>{$row['keywords']}</td>";echo "<td>{$row['autor']}</td>";echo "<td>{$row['addtime']}</td>";echo "<td>{$row['content']}</td>";echo "<td><a href='javascript:del({$row['id']})'>刪除</a><a href='editnews.php?id={$row['id']}'>修改</a> </td>";echo "</tr>";}// 5. 釋放結(jié)果集mysql_free_result($result);mysql_close($link);?></table></div><script type="text/javascript">function del (id) {if (confirm("確定刪除這條新聞嗎?")){window.location = "action-del.php?id=" id;}}</script></body></html>頁(yè)面如圖:
2. 增加新聞
2.1 點(diǎn)擊增加按鈕,通過頁(yè)面addnews.html添加數(shù)據(jù)
<!doctype html> <html> <head> <meta charset="utf-8"> <title>添加新聞</title> </head><style type="text/css">form{margin: 20px;}</style><body><form action="action-addnews.php" method="post"> <label>標(biāo)題:</label><input type="text" name="title"> <label>關(guān)鍵字:</label><input type="text" name="keywords"> <label>作者:</label><input type="text" name="autor"> <label>發(fā)布時(shí)間:</label><input type="date" name="addtime"> <label>內(nèi)容:</label><input type="text" name="content"> <input type="submit" value="提交"> </form> </body> </html>2.2 創(chuàng)建處理增加新聞的服務(wù)端文件action-addnews.php
<?php// 處理增加操作的頁(yè)面 require "dbconfig.php";// 連接mysql$link = @mysql_connect(host,user,pass) or die("提示:數(shù)據(jù)庫(kù)連接失敗!");// 選擇數(shù)據(jù)庫(kù)mysql_select_db(dbname,$link);// 編碼設(shè)置mysql_set_charset(&
上一個(gè):鐵觀音有關(guān)常識(shí)
下一個(gè):新裝的ssd無法識(shí)別,新裝機(jī)電腦無法識(shí)別ssd硬盤

喝普洱茶引起的不良反應(yīng)
沙漠拍婚紗照哪里拍?注意事項(xiàng)有哪些?
影響網(wǎng)站做SEO優(yōu)化的幾個(gè)注意事項(xiàng)
通信電纜HYAT通信電纜-400*2*0.5廠家
中國(guó)惠普官方網(wǎng)站網(wǎng)址,惠普打印機(jī)官方網(wǎng)站網(wǎng)址是多少
工程建設(shè)定額的分類有哪些?
windows10更新怎樣關(guān)閉(win10 更新怎么關(guān)閉)
鉑熱電阻常識(shí)
怎樣才能挑到一款讓你滿意的小青柑?
怎樣使新栽小龍柏安全越冬
十八禁 网站在线观看免费视频_2020av天堂网_一 级 黄 色 片免费网站_绝顶高潮合集Videos