php實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查
1.查詢:
數(shù)據(jù)的顯示,這里就可以嵌入php來進(jìn)行數(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>無標(biāo)題文檔</title>
</head>
<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代號(hào)</td>
<td>名稱</td>
<td>性別</td>
<td>生日</td>
<td>班級(jí)</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>
//添加一個(gè)單擊事件,防止不小心刪掉
<td><a onclick=\"return confirm('確定要?jiǎng)h除嗎???')"
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.刪除的處理頁面
刪除時(shí)是鏈接到刪除處理頁面的,所以還要寫一個(gè)刪除處理頁面:
<?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ù):
點(diǎn)擊
即可進(jìn)入添加頁面
添加頁面:
<body>
<h1>添加</h1>
<form action="add.php" method="post" >
<div>代號(hào):<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)建表時(shí)性別是用的1或2來表示的,要是進(jìn)行修改不知道1或2代表了什么,所以就要進(jìn)行處理,處理成用戶能夠明白的男和女
<div>班級(jí):
<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>
添加也需要一個(gè)處理頁面來判斷添加:
<?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 報(bào)頭。
}
else {
echo "添加失敗";
}
?>
效果圖:

4.修改數(shù)據(jù):主鍵不可修改?。?/strong>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</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>代號(hào):<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'":""; ?>/>男 <!-- 三元運(yùn)算符,如果性別=ture,默認(rèn)值就在男上面,否則空--> <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>班級(jí):<select name="class">
//value取默認(rèn)值
<?php
$sclass = "select * from class";
$rclass = $db->query($sclass);
while($attr = $rclass->fetch_row())
//取到的班級(jí)信息
{
//判斷將要輸出的班級(jí)是不是和該人員的是否相同
if($arr[4]==$attr[0])//arr是班級(jí)名,attr是班級(jí)的代號(hào),倆表
{
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 "修改失敗";
}
?>
修改的效果圖:

- 利用PHP訪問MySql數(shù)據(jù)庫的邏輯操作以及增刪改查的實(shí)例講解
- thinkPHP數(shù)據(jù)庫增刪改查操作方法實(shí)例詳解
- PHP簡單數(shù)據(jù)庫操作類實(shí)例【支持增刪改查及鏈?zhǔn)讲僮鳌?/a>
- thinkphp3.2.3版本的數(shù)據(jù)庫增刪改查實(shí)現(xiàn)代碼
- PHP連接數(shù)據(jù)庫實(shí)現(xiàn)注冊(cè)頁面的增刪改查操作
- php中PDO方式實(shí)現(xiàn)數(shù)據(jù)庫的增刪改查
- php數(shù)據(jù)庫的增刪改查 php與javascript之間的交互
相關(guān)文章
關(guān)于PHP轉(zhuǎn)換超過2038年日期出錯(cuò)的問題解決
這篇文章主要給大家介紹了關(guān)于PHP轉(zhuǎn)換超過2038年日期出錯(cuò)問題的解決方法,文中給出了詳細(xì)的解決方法,通過示例代碼讓大家更容易理解和學(xué)習(xí),需要的朋友們下面來一起看看吧。2017-06-06
PHP數(shù)據(jù)庫調(diào)用類調(diào)用實(shí)例(詳細(xì)注釋)
PHP開發(fā)中我們經(jīng)常需要用一些數(shù)據(jù)庫類,這里簡單的分享下調(diào)用類的代碼,學(xué)習(xí)php數(shù)據(jù)庫操作的朋友可以參考下2012-07-07
php格式化時(shí)間戳顯示友好的時(shí)間實(shí)現(xiàn)思路及代碼
顯示為2014-10-20 10:22顯得很呆板,那么用php怎么實(shí)現(xiàn)友好的時(shí)間格式呢?下面將思路與實(shí)現(xiàn)代碼與大家分享下2014-10-10
PHP API接口必備之輸出json格式數(shù)據(jù)示例代碼
這篇文章主要給大家介紹了關(guān)于PHP API接口必備之輸出json格式數(shù)據(jù)的相關(guān)資料文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-06-06
PHP實(shí)現(xiàn)的oracle分頁函數(shù)實(shí)例
這篇文章主要介紹了PHP實(shí)現(xiàn)的oracle分頁函數(shù),結(jié)合實(shí)例形式分析了PHP針對(duì)oracle數(shù)據(jù)庫使用rownum代替MySQL中l(wèi)imit實(shí)現(xiàn)的分頁操作相關(guān)技巧,需要的朋友可以參考下2016-01-01
php使用Cookie實(shí)現(xiàn)和用戶會(huì)話的方法
這篇文章主要介紹了php使用Cookie實(shí)現(xiàn)和用戶會(huì)話的方法,分析了Cookie的原理、設(shè)置與使用技巧,需要的朋友可以參考下2015-01-01

