程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 數據庫添加、修改、刪除基礎操作代碼(1/4)

數據庫添加、修改、刪除基礎操作代碼(1/4)

編輯:關於PHP編程

數據庫添加、修改、刪除基礎操作代碼 這是一款比較適合php初學者學的教程哦,我們利用一個簡單的實例來對數據庫添加、修改、刪除哦,這樣更系統的讓各位知道php mysql數據庫操作的要點。

數據庫教程添加、修改、刪除基礎操作代碼
這是一款比較適合php教程初學者學的教程哦,我們利用一個簡單的實例來對數據庫添加、修改、刪除哦,這樣更系統的讓各位知道php mysql教程數據庫操作的要點。
*/
  require_once('common.php');
  $action = $_get['action'];
?>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.bKjia.c0m/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>人才列表</title>
<link href="style.css教程" type="text/css" rel="stylesheet" />
</head>
<body> 
 <div id="wrap">
 <div id="main">
 <?php
      if($action=='add'){
 ?>
    <form action="?action=save" method="post" name="form1">
  <table width="300" border="0" cellspacing="0" cellpadding="0"  class="post">
          <tr>
            <td colspan="2">添加人員</td></tr>
          <tr><td width="78">登陸賬號</td>
            <td width="220"><input name="login" type="text" id="login" /></td>
          </tr>
          <tr>
            <td>登陸密碼</td>
            <td><input name="pws" type="text" id="pws" /></td>
          </tr>
          <tr>
            <td>問題</td>
            <td><input name="question" type="text" id="question" /></td>
          </tr>
           <tr>
            <td>答案</td>
            <td><input name="answer" type="text" id="answer" /></td>
          </tr>
            <tr>
              <td colspan="2"><input name="button" type="submit" id="button" value=" 添加 " /></td>
            </tr>
             
      </table>
    </form>
 <?php  
      }
      elseif($action=='save'){
    $login = isset($_post['login']) ? $_post['login'] : '';
    $pws = isset($_post['pws']) ? $_post['pws'] : '';
    $question = isset($_post['question']) ? $_post['question'] : '';
    $answer = isset($_post['answer']) ? $_post['answer'] : '';
    $sql = "insert into person (login,pws,question,answer)
    values('$login','$pws','$question','$answer')";
    $db->query($sql);
    forward('發布成功','href','personlist.php');
          }

    elseif($action=='del'){
    $person_id=$_get['person_id'];
    $page=$_get['page'];
    $sql="delete from person where person_id='$person_id'";
    $db->query($sql);
    forward('刪除成功','href','personlist.php?page='.$page);
          } 
    elseif($action=='editsave'){
    $person_id = isset($_post['person_id']) ? $_post['person_id'] : '';
    $page = isset($_post['page']) ? $_post['page'] : '';
          $login = isset($_post['login']) ? $_post['login'] : '';
    $pws = isset($_post['pws']) ? $_post['pws'] : '';
    $question = isset($_post['question']) ? $_post['question'] : '';
    $answer = isset($_post['answer']) ? $_post['answer'] : '';
    $sql="update person set login='$login',pws='$pws',question='$question',answer='$answer' where person_id='$person_id'";
    $db->query($sql);
          forward('修改成功','href','personlist.php?page='.$page);
          }
   elseif($action=='edit'){
    $person_id=$_get['person_id'];
    $page=$_get['page'];
    $sql="select * from person where person_id='$person_id'";
    $query = $db->query($sql);
    $row = $db->fetch_array($query);
    $login=$row['login'];
    $pws=$row['pws'];
    $question=$row['question'];
    $answer=$row['answer'];
       ?>
    <form action="?action=editsave" method="post" name="form1">
  <table width="300" border="0" cellspacing="0" cellpadding="0"  class="post">
          <tr>
          <input name="page" type="hidden"  value="<?php echo $page?>"/>
          <input name="person_id" type="hidden"  value="<?php echo $person_id?>"/>
            <td colspan="2">修改人員</td></tr>
          <tr><td width="78">登陸賬號</td>
            <td width="220"><input name="login" type="text" id="login"  value="<?php echo $login?>"/></td>
          </tr>
          <tr>
            <td>登陸密碼</td>
            <td><input name="pws" type="text" id="pws" value="<?php echo $pws?>"/></td>
          </tr>
          <tr>
            <td>問題</td>
            <td><input name="question" type="text" id="question" value="<?php echo $question?>"/></td>
          </tr>
           <tr>
            <td>答案</td>
            <td><input name="answer" type="text" id="answer" value="<?php echo $answer?>"/></td>
          </tr>
            <tr>
              <td colspan="2"><input name="button" type="submit" id="button" value=" 修改 " /></td>
            </tr>
             
      </table>
    </form>
 <?php 
          }
      else{
          $page = isset($_get['page']) ?intval($_get['page']) : 1;
          $num = 5;
          $sql="select * from person";
          $query = $db->query($sql);
          $totalnum = $db->num_rows($query);//記錄總數
          $pagenum = ceil($totalnum/$num); //總頁數
          $offset = ($page-1) * $num;
          $sql=$sql." limit $offset,$num ";
          $query = $db->query($sql);//取得記錄
        ?>
          <table width="639" border="0" cellspacing="0" cellpadding="0"  class="post">
          <tr>
            <td colspan="6">記錄總數:<?php echo $totalnum;?>————<a href="personlist.php?action=add">添加人員</a></td></tr>
          <tr><td width="126">登陸賬號</td>
          <td width="98">登陸密碼</td>
          <td width="115">問題</td>
          <td width="66">答案</td>
          <td width="138">加入時間</td>
          <td width="94">操作</td>
          </tr>
  <?php
        while ($row = $db->fetch_array($query)) {
        ?>
          <tr>
          <td><?php echo $row['login'];?></td>
            <td><?php echo $row['pws'];?></td>
            <td><?php echo $row['question'];?></td>
            <td><?php echo $row['answer'];?></td>
            <td><?php echo $row['addtime'];?></td>
            <td><a href="?action=del&person_id=<?php echo $row['person_id'] ?>&page=<?php echo $page ?>">刪除</a>/
            <a href="?action=edit&person_id=<?php echo $row['person_id'] ?>&page=<?php echo $page ?>">修改</a></td>
          </tr>
       <?php
          }
        ?>
    </table>
   </div>
       <div id="pages_btns">
            <div class="pages"><?php showpage($page, $num, $pagenum, $totalnum)?></div>
   </div>
   <?php
   }
   ?>
</div>
</body>
</html>

1 2 3 4

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved