php實現(xiàn)數(shù)據(jù)庫的增刪改查
1.查詢:
數(shù)據(jù)的顯示,這里就可以嵌入php來進行數(shù)據(jù)的輸出
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <table width="100%" border="1" cellpadding="0" cellspacing="0"> <tr> <td>代號</td> <td>名稱</td> <td>性別</td> <td>生日</td> <td>班級</td> <td>操作</td> </tr> <?php $db = new MySQLi("localhost","root","12345678","heiheihei"); //連接數(shù)據(jù)庫 $sql = "select * from student "; //寫sql語句 $r = $db->query($sql); //執(zhí)行sql語句返回給r if($r)//條件 { while ($attr = $r->fetch_row()) { $ssex = ""; if($attr[2]) { $ssex = "男"; } else { $ssex = "女"; } echo " <tr> <td>{$attr[0]}</td> <td>{$attr[1]}</td> <td>{$ssex}</td> <td>{$attr[3]}</td> <td>{$attr[4]}</td> //添加一個單擊事件,防止不小心刪掉 <td><a onclick=\"return confirm('確定要刪除嗎???')" href='shanchu.php?sno={$attr[0]}'>刪除</a> <a href='xiugai.php?sno={$attr[0]}'>修改</a> </td> </tr>"; } } ?> </table> <a href="tianjia.php" rel="external nofollow" >添加頁面</a> </body> </html>
2.刪除的處理頁面
刪除時是鏈接到刪除處理頁面的,所以還要寫一個刪除處理頁面:
<?php $aaa = $_GET ["sno"]; //刪除方式使用的get,照舊 $db = new mysqli("localhost","root","12345678","heiheihei"); //連接... $sql = "delete from student WHERE sno='{$aaa}'"; //寫sql語句,sno主鍵 if($db->query($sql)) //執(zhí)行sql語句 { header("location:text.php"); //刪完回去表頁面 } else{ echo "刪除失敗"; } ?>
來張效果圖:
3.添加數(shù)據(jù):
點擊即可進入添加頁面
添加頁面:
<body> <h1>添加</h1> <form action="add.php" method="post" > <div>代號:<input type="text" name="sno"/></div> <div>名字:<input type="text" name="sname"/></div> <div>性別: <input type="radio" value="1" name="sex" />男 <input type="radio" value="0" name="sex"/>女</div> <div>日期:<input type="text" name="sbirthday"/></div> //創(chuàng)建表時性別是用的1或2來表示的,要是進行修改不知道1或2代表了什么,所以就要進行處理,處理成用戶能夠明白的男和女 <div>班級: <select name="class"> <?php $db= new MYSQLi("localhost","root","12345678","heiheihei"); //連接... $sql = " select * from class "; //寫sql... $r = $db->query($sql); //執(zhí)行...返回... while($arr = $r->fetch_row()) { echo "<option value='{$arr[0]}'>{$arr[1]}</option>"; //添上以后回表頁面 } ?> </select> </div> <div><input type="submit" value="添加"/></div> </form> </body>
添加也需要一個處理頁面來判斷添加:
<?php $sno = $_POST["sno"]; //$_POST 變量用于收集來自 method="post" 的表單中的值。 $sname = $_POST["sname"]; $ssex = $_POST["ssex"]; $sbirthday = $_POST["sbirthday"]; $class = $_POST["class"]; $db = new mysqli("localhost","root","12345678","heiheihei"); $sql = "insert into student VALUES ('{$sno}','{$sname}','{$ssex}','{$sbirthday}','{$class}')"; //向數(shù)據(jù)庫中添加寫的數(shù)據(jù) if($db->query($sql)) { header("location:text.php"); //header() 函數(shù)向客戶端發(fā)送原始的 HTTP 報頭。 } else { echo "添加失敗"; } ?>
效果圖:
4.修改數(shù)據(jù):主鍵不可修改!!
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <h1>修改</h1> <?php $sno = $_GET{"sno"}; $db = new mysqli("localhost","root","12345678","heiheihei"); $sql = "select * from student WHERE sno='{$sno}'"; $r = $db->query($sql); $arr = $r->fetch_row(); ?> <form action="update.php" method="post"> <div>代號:<input readonly="readonly" type="text" name="sno" value="<?php echo $arr[0]; ?>"/></div> //readonly只可讀 <div>名稱:<input type="text" name="sname" value="<?php echo $arr[1]; ?>"/></div> <div>性別: <input type="radio" name="ssex" value="1" <?php echo $arr[2]?"checked='checked'":""; ?>/>男 <!-- 三元運算符,如果性別=ture,默認值就在男上面,否則空--> <input type="radio" name="ssex" value="0" <?php echo $arr[2]?"":"checked='checked'"; ?>/>女 </div> <div>日期:<input type="text" name="sbirthday" value="<?php echo $arr[3]; ?>"/></div> <div>班級:<select name="class"> //value取默認值 <?php $sclass = "select * from class"; $rclass = $db->query($sclass); while($attr = $rclass->fetch_row()) //取到的班級信息 { //判斷將要輸出的班級是不是和該人員的是否相同 if($arr[4]==$attr[0])//arr是班級名,attr是班級的代號,倆表 { echo "<option value = '{$attr[0]}' selected='selected'>{$attr[1]}</option>"; } else{ echo "<option value = '{$attr[0]}'>{$attr[1]}</option>"; } } ?> </select></div> <div><input type="submit" value="修改完畢"/></div> </form> </body> </html>
]
修改的處理頁面:
<?php $sno = $_POST["sno"]; $sname = $_POST["sname"]; $ssex = $_POST["ssex"]; $sbirthday = $_POST["sbirthday"]; $class = $_POST["class"]; $db = new mysqli("localhost","root","12345678","heiheihei"); $sql = "update student set sname='{$sname}', ssex='{$ssex}', sbirthday='{$sbirthday}', class='{$class}' WHERE sno='{$sno}'"; //看一下是不是傳遞過來的sno值; if($db->query($sql)) { header("location:text.php"); } else{ echo "修改失敗"; } ?>
修改的效果圖:
相關(guān)文章
php中switch與ifelse的效率區(qū)別及適用情況分析
這篇文章主要介紹了php中switch與ifelse的效率區(qū)別及適用情況分析,以實例的形式分析了針對變量與常量的情況下switch與ifelse的效率區(qū)別,非常具有實用價值,需要的朋友可以參考下2015-02-02javascript數(shù)組與php數(shù)組的地址傳遞及值傳遞用法實例
這篇文章主要介紹了javascript數(shù)組與php數(shù)組的地址傳遞及值傳遞用法,實例分析了javascript與php的數(shù)組使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-01-01PHP 使用header函數(shù)設置HTTP頭的示例解析 表頭
本篇文章是對PHP使用header函數(shù)設置HTTP頭的示例進行了詳細的分析介紹,需要的朋友參考下2013-06-06WordPress中注冊菜單與調(diào)用菜單的方法詳解
這篇文章主要介紹了WordPress中注冊菜單與調(diào)用菜單的方法詳解,分別依靠register_nav_menus()函數(shù)與wp_nav_menu()函數(shù)的使用,需要的朋友可以參考下2015-12-12php實現(xiàn)圖片上傳并利用ImageMagick生成縮略圖
這篇文章主要為大家詳細介紹了php實現(xiàn)圖片上傳并利用ImageMagick生成縮略圖的相關(guān)資料,需要的朋友可以參考下2016-03-03PHP錯誤Warning:mysql_query()解決方法
這篇文章主要介紹了PHP錯誤Warning:mysql_query()的解決方法,希望可以真正解決大家的問題,需要的朋友可以參考下2015-10-10