亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

php實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查

 更新時(shí)間:2017年02月26日 15:23:47   作者:我之姓冠你之名  
本文給大家介紹的是PHP連接數(shù)據(jù)庫(kù)以及實(shí)現(xiàn)數(shù)據(jù)庫(kù)的增刪改查功能的方法及示例代碼,希望對(duì)大家學(xué)習(xí)php能夠有所幫助

1.查詢:

數(shù)據(jù)的顯示,這里就可以嵌入php來(lái)進(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>無(wú)標(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ù)庫(kù)
$sql = "select * from student ";
//寫(xiě)sql語(yǔ)句
$r = $db->query($sql);
//執(zhí)行sql語(yǔ)句返回給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" >添加頁(yè)面</a>
</body>

</html>

2.刪除的處理頁(yè)面

刪除時(shí)是鏈接到刪除處理頁(yè)面的,所以還要寫(xiě)一個(gè)刪除處理頁(yè)面:

<?php
$aaa = $_GET ["sno"]; //刪除方式使用的get,照舊
$db = new mysqli("localhost","root","12345678","heiheihei");
//連接...
$sql = "delete from student WHERE sno='{$aaa}'";
//寫(xiě)sql語(yǔ)句,sno主鍵
if($db->query($sql)) //執(zhí)行sql語(yǔ)句
{
  header("location:text.php");
//刪完回去表頁(yè)面
}
else{
  echo "刪除失敗";
}
?>

來(lái)張效果圖:

3.添加數(shù)據(jù):

點(diǎn)擊即可進(jìn)入添加頁(yè)面

添加頁(yè)面:

<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來(lái)表示的,要是進(jìn)行修改不知道1或2代表了什么,所以就要進(jìn)行處理,處理成用戶能夠明白的男和女

  <div>班級(jí):
  <select name="class">

  <?php
    $db= new MYSQLi("localhost","root","12345678","heiheihei");
//連接...
    $sql = " select * from class ";
//寫(xiě)sql...    

    $r = $db->query($sql);
//執(zhí)行...返回...
    while($arr = $r->fetch_row())
    {
      echo "<option value='{$arr[0]}'>{$arr[1]}</option>";
      //添上以后回表頁(yè)面
    }
    ?>

 
  </select>
  </div>
  <div><input type="submit" value="添加"/></div>
</form>

</body>

添加也需要一個(gè)處理頁(yè)面來(lái)判斷添加:

<?php
$sno = $_POST["sno"];
//$_POST 變量用于收集來(lái)自 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ù)庫(kù)中添加寫(xiě)的數(shù)據(jù)
  if($db->query($sql))
{
  header("location:text.php");
  //header() 函數(shù)向客戶端發(fā)送原始的 HTTP 報(bào)頭。
}
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>無(wú)標(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>

]

修改的處理頁(yè)面:

<?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}'";
//看一下是不是傳遞過(guò)來(lái)的sno值;
if($db->query($sql))
{
  header("location:text.php");
}
else{
  echo "修改失敗";
}



?>

修改的效果圖:

相關(guān)文章

最新評(píng)論