一聚教程网:一个值得你收藏的教程网站

热门教程

数据库添加、修改、删除基础操作代码

时间:2022-06-24 19:10:18 编辑:袖梨 来源:一聚教程网

  require_once('common.php');
  $action = $_get['action'];
?>

 代码如下 复制代码





人才列表
css教程" type="text/css" rel="stylesheet" />

 
 


 

        if($action=='add'){
 ?>
   

 
         
           
         
           
         
         
           
           
         
         
           
           
         
          
           
           
         
           
             
           
             
     
添加人员
登陆账号
登陆密码
问题
答案

   

        }
      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'];
       ?>
   


 
         
         
         
           
         
           
         
         
           
           
         
         
           
           
         
          
           
           
         
           
             
           
             
     
修改人员
登陆账号
登陆密码
问题
答案

   

            }
      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);//取得记录
        ?>
         
         
           
         
         
         
         
         
         
         
           while ($row = $db->fetch_array($query)) {
        ?>
         
         
           
           
           
           
           
         
                 }
        ?>
   
记录总数:————添加人员
登陆账号登陆密码问题答案加入时间操作
删除/
            修改

  

      

           

  

      }
   ?>


 

 
config.php

 

 代码如下 复制代码
$host = 'localhost';
$user = 'root';
$pass = '123456';
$db   = 'rc';
?>

common.php

 代码如下 复制代码
  require_once('config.php');
  require_once('mysql.php');
  require_once('function.php');
 
  $db = new mysql($host, $user, $pass, $db);
?>

function.php

 代码如下 复制代码

/**
 * 分页函数显示
 * date : 2008-12-6
 *
 * @param $page 当前页数
 * @param $num 每页显示数
 * @param $pagenum 分页总数
 * @param $totalnum 记录总数
 * 程序调用:showpage(当前页数,每页显示数,分页总数,记录总数);
 */
function showpage($page, $num, $pagenum, $totalnum) {
        $maxto = 5; //每次显示页数
        $nextpage = $page + 1;
        if ($nextpage > $pagenum) $nextpage = $pagenum;
        $for_end = ($pagenum > ($page + $maxto)) ? ($page +$maxto) : $pagenum;
        $for_begin = (($page - $maxto)>1) ? ($page - $maxto) : 1;
        echo " total: $totalnum ";  
        for ($i = $for_begin; $i <= $for_end; $i++) {
         if ($i != $page){
          echo "$i ";
         } else {
          echo "$i";
         }
        }
        echo "... $pagenum";
        echo "";
}
/*网页特效提示框*/
function forward($msg, $methd='', $url = ''){
     $sstr = "";
     die($sstr);
}

?>

 

 
mysql.php数据库连接

 

 代码如下 复制代码

/**
 * ###################################################
 *
 * 数据库操作类
 * ###################################################
 */
 
class mysql {

 var $user,$pass,$host,$db;
 var $id,$data,$fields,$row,$row_num,$insertid,$version,$query_num=0;

 function mysql($host,$user,$pass,$db)
 {
  $this->host = $host;
  $this->pass = $pass;
  $this->user = $user;
  $this->db = $db;
  $this->dbconnect($this->host, $this->user, $this->pass);
  $this->selectdb($this->db);
  if($this->version() >'4.1')
  mysql_query("set names 'gbk'");
 }//初始化对象
 function dbconnect($host,$user,$pass)
 {
  $this->id = @ mysql_connect($host,$user,$pass) or
  sysmsg("连接数据库失败,可能是mysql数据库用户名或密码错误");
 }
 function selectdb($db)
 {
  @ mysql_select_db($db,$this->id) or sysmsg("未找到指定数据库");
 }

 function query($sql)
 {
  $query = @ mysql_query($sql,$this->id) or sysmsg("sql语句执行错误:$sql
".$this->geterror());
  $this->query_num();
  return $query;
 }

 function fetch_array($query)
 {
  $this->data = @mysql_fetch_array($query);
  return $this->data;
 }
 function query_num()
 {
  $this->query_num++;
 }
 function num_fields($query)
 {
  $this->fields = @mysql_num_fields($query);
  return $this->fields;
 }
 function fetch_row($query)
 {
  $this->row = @mysql_fetch_row($query);
  return $this->row;
 }

 function num_rows($query)
 {
  $this->row_num = @mysql_num_rows($query);
  return $this->row_num;
 }

 function insert_id()
 {
  $this->insertid = mysql_insert_id();
  return $this->insertid;
 }
 function version()
 {
  $this->version = mysql_get_server_info();
  return $this->version;
 }
 function fetch_one_array($sql)
 {
  $query = $this->query($sql);
  $this->data = $this->fetch_array($query);
  return $this->data;
 }
 function geterror()
 {
  return mysql_error();
 }
}
?>

 

 
//sql数据库

 

 代码如下 复制代码

-- phpmyadmin sql dump
-- version 3.1.2-rc1
-- http://www.phpmyadmin.net
--
-- 主机: localhost
-- 生成日期: 2009 年 04 月 15 日 09:22
-- 服务器版本: 5.0.67
-- php 版本: 5.2.6

set sql_mode="no_auto_value_on_zero";


/*!40101 set @old_character_set_client=@@character_set_client */;
/*!40101 set @old_character_set_results=@@character_set_results */;
/*!40101 set @old_collation_connection=@@collation_connection */;
/*!40101 set names utf8 */;

--
-- 数据库: `rc`
--

-- --------------------------------------------------------

--
-- 表的结构 `person`
--

create table if not exists `person` (
  `person_id` int(11) unsigned not null auto_increment,
  `login` varchar(25) not null,
  `pws` varchar(25) not null,
  `question` varchar(25) not null,
  `answer` varchar(25) not null,
  `addtime` timestamp not null default current_timestamp,
  primary key  (`person_id`)
) engine=innodb  default charset=gbk comment='人员表' auto_increment=13 ;

--
-- 导出表中的数据 `person`
--

insert into `person` (`person_id`, `login`, `pws`, `question`, `answer`, `addtime`) values
(1, 'huangxulei', '123456', 'whoareyou', 'iami', '2009-04-15 10:06:42'),
(3, 'lihuang', '123456', '123', 'www.111com.net', '2009-04-15 10:47:26'),
(4, '2222', '333', '3333', '33333', '2009-04-15 14:23:50'),
(8, '张红', '123456', 'whoareyou', '000', '2009-04-15 16:18:09'),
(9, '111', '111', '111', '111', '2009-04-15 16:26:07'),
(10, '111', '111', '111', '111', '2009-04-15 16:26:07'),
(11, '5555555555', '00000', '11100', '33333333', '2009-04-15 16:26:07');

热门栏目