php數據庫的增刪改查

1.查詢:php

 

數據的顯示,這裏就能夠嵌入php來進行數據的輸出html

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<!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" );
//鏈接數據庫
$sql  "select * from student " ;
//寫sql語句
$r  $db ->query( $sql );
//執行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.刪除的處理頁面mysql

刪除時是連接到刪除處理頁面的,因此還要寫一個刪除處理頁面:sql

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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 ))  //執行sql語句
{
   header( "location:text.php" );
//刪完回去表頁面
}
else {
   echo  "刪除失敗" ;
}
?>

 

 

來張效果圖:數據庫

php數據庫的增刪改查

3.添加數據:函數

點擊php數據庫的增刪改查便可進入添加頁面post

添加頁面:fetch

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<body>
 
<h1>添加</h1>
<form action= "add.php"  method= "post"  >
   <p>代號:<input type= "text"  name= "sno" /></p>
   <p>名字:<input type= "text"  name= "sname" /></p>
   <p>性別: <input type= "radio"  value= "1"  name= "sex"  />男
     <input type= "radio"  value= "0"  name= "sex" />女</p>
   <p>日期:<input type= "text"  name= "sbirthday" /></p>
//建立表時性別是用的1或2來表示的,要是進行修改不知道1或2表明了什麼,因此就要進行處理,處理成用戶可以明白的男和女
 
   <p>班級:
   <select name= "class" >
 
   <?php
     $db new  MYSQLi( "localhost" , "root" , "12345678" , "heiheihei" );
//鏈接...
     $sql  " select * from class " ;
//寫sql...   
 
     $r  $db ->query( $sql );
//執行...返回...
     while ( $arr  $r ->fetch_row())
     {
       echo  "<option value='{$arr[0]}'>{$arr[1]}</option>" ;
       //添上之後回表頁面
     }
     ?>
 
  
   </select>
   </p>
   <p><input type= "submit"  value= "添加" /></p>
</form>
 
</body>

 

添加也須要一個處理頁面來判斷添加:ui

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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}')" ;
//向數據庫中添加寫的數據
   if ( $db ->query( $sql ))
{
   header( "location:text.php" );
   //header() 函數向客戶端發送原始的 HTTP 報頭。
}
else  {
   echo  "添加失敗" ;
}
 
 
?>

 

 

效果圖:spa

php數據庫的增刪改查

4.修改數據:主鍵不可修改!!

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<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" >
   <p>代號:<input readonly= "readonly"  type= "text"  name= "sno"  value="<?php
echo  $arr [0];
?>"/></p>
 
//readonly只可讀
  <p>名稱:<input type= "text"  name= "sname"  value= "<?php echo $arr[1]; ?>" /></p> <p>性別: <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'" ; ?>/>女 </p> <p>日期:<input type= "text"  name= "sbirthday"  value= "<?php echo $arr[3]; ?>" /></p> <p>班級:<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></p>
   <p><input type= "submit"  value= "修改完畢" /></p>
 
</form>
 
 
</body>
</html>

 

]

修改的處理頁面:

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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數據庫的增刪改查

相關文章
相關標籤/搜索