> php教程 > php手册 > 본문

使用php完成一个用户注册以及管理的demo(一)

WBOY
풀어 주다: 2016-06-06 19:42:20
원래의
1404명이 탐색했습니다.

1.设计mysql的用户表 2.设计文件结构 3.制作后台增加用户的界面 4.对于数据库操作的封装,用来操作数据库的一些方式 5. include.php的制作 (1)用户表的设计 mysql数据库 / 如果用户表user已经存在的话,则删除掉 / DROP TABLE IF EXISTS user ; / !40101 S

1.设计mysql的用户表
2.设计文件结构
3.制作后台增加用户的界面
4.对于数据库操作的封装,用来操作数据库的一些方式
5. include.php的制作


(1)用户表的设计 mysql数据库

/如果用户表user已经存在的话,则删除掉/
DROP TABLE IF EXISTS user;
/!40101 SET @saved_cs_client = @@character_set_client /;
/!40101 SET character_set_client = utf8 /;
CREATE TABLE user (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
username varchar(20) NOT NULL,
password char(32) NOT NULL,
sex enum(‘男’,’女’,’保密’) NOT NULL DEFAULT ‘保密’,
email varchar(50) NOT NULL,
face varchar(50) NOT NULL,
/注册时间/
regTime int(10) unsigned NOT NULL,
/活跃程度/
activeFlag tinyint(1) DEFAULT ‘0’,
PRIMARY KEY (id),
/用户名不允许重复/
UNIQUE KEY username (username)
)
/自增长从编号6开始,默认编码为utf-8/
ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
(2)文件的目录结构
使用php完成一个用户注册以及管理的demo(一)
其中addUser把数据提交到doAdminAction.php页面中,在这个页面中调用core(主干,主要)文件夹里的admin.inc.php中的函数操作与用户有关的操作,lib文件夹里的文件里的函数 mysql.func.php封装了与数据库有关的函数,page.func.php封装了与分页有关的函数(凡是带test的是我自己用来测试的,不用管了(>_ (3)制作后台增加用户的界面

<code><span>
<span>html</span>></span>
<span>head</span>>
<span>meta</span> <span>charset</span>=<span>"utf-8"</span>>
<span>title</span>><span><span>title</span>></span>
<span><span>head</span>></span>
<span>body</span>>
<span>H3</span>>添加管理员<span><span>H3</span>></span>
<span>FORM</span>  <span>action</span>=<span>"doAdminAction.php?act=addUser"</span> <span>method</span>=<span>"post"</span>>
<span>TABLE</span> <span>width</span>=<span>"60%"</span> <span>border</span>=<span>"1"</span> <span>cellpadding</span>=<span>"5"</span> <span>cellspacing</span>=<span>"0"</span> <span>bgcolor</span>=<span>"#cccccc"</span>>
<span>tr</span>>
    <span>TD</span> <span>align</span>=<span>"right"</span>>用户名称<span><span>TD</span>></span>
    <span>TD</span>><span>input</span> <span>type</span>=<span>"text"</span> <span>name</span>=<span>"username"</span> <span>placeholder</span>=<span>"请输入用户名称"</span>/><span><span>TD</span>></span>
<span><span>tr</span>></span>
<span>tr</span>>
    <span>TD</span> <span>align</span>=<span>"right"</span>>用户密码<span><span>TD</span>></span>
    <span>TD</span>><span>input</span> <span>type</span>=<span>"text"</span> <span>name</span>=<span>"password"</span> <span>placeholder</span>=<span>"请输入用户名称"</span>/><span><span>TD</span>></span>
<span><span>tr</span>></span>
<span>tr</span>>
    <span>TD</span> <span>align</span>=<span>"right"</span>>用户邮箱<span><span>TD</span>></span>
    <span>TD</span>><span>input</span> <span>type</span>=<span>"text"</span> <span>name</span>=<span>"email"</span> <span>placeholder</span>=<span>"请输入用户名称"</span>/><span><span>TD</span>></span>
<span><span>tr</span>></span>
<span>tr</span>>
    <span>td</span> <span>align</span>=<span>"right"</span>>性别<span><span>td</span>></span>
    <span>td</span>><span>input</span> <span>type</span>=<span>"radio"</span> <span>name</span>=<span>"sex"</span> <span>value</span>=<span>"1"</span> <span>checked</span>=<span>"checked"</span>/>男
    <span>input</span> <span>type</span>=<span>"radio"</span> <span>name</span>=<span>"sex"</span> <span>value</span>=<span>"2"</span> />女
    <span>input</span> <span>type</span>=<span>"radio"</span> <span>name</span>=<span>"sex"</span> <span>value</span>=<span>"3"</span> />保密
    <span><span>td</span>></span>
<span><span>tr</span>></span>
<span>tr</span>>
        <span>td</span> <span>align</span>=<span>"right"</span>>头像<span><span>td</span>></span>
        <span>td</span>><span>input</span> <span>type</span>=<span>"file"</span> <span>name</span>=<span>"face"</span> /><span><span>td</span>></span>
<span><span>tr</span>></span>
<span>tr</span>>
    <span>TD</span> <span>colspan</span>=<span>"2"</span>><span>input</span> <span>type</span>=<span>"submit"</span> <span>value</span>=<span>"添加用户"</span>/><span><span>TD</span>></span>
<span><span>tr</span>></span>
<span><span>FORM</span>></span>
<span><span>body</span>></span>
<span><span>html</span>></span></code>
로그인 후 복사

效果如图所示使用php完成一个用户注册以及管理的demo(一)
(4)数据库的封装


<code><span><span><?php </span> 
<span>/**
 * 连接数据库
 *<span> @return</span> resource
 */</span>
<span><span>function</span> <span>connect</span><span>()</span>{</span>
    <span>$link</span>=mysql_connect(DB_HOST,DB_USER,DB_PWD) <span>or</span> <span>die</span>(<span>"数据库连接失败Error:"</span>.mysql_errno().<span>":"</span>.mysql_error());
    mysql_set_charset(DB_CHARSET);
    mysql_select_db(DB_DBNAME) <span>or</span> <span>die</span>(<span>"指定数据库打开失败"</span>);
    <span>return</span> <span>$link</span>;
}

<span>/**
 * 完成记录插入的操作
 *<span> @param</span> string $table
 *<span> @param</span> array $array
 *<span> @return</span> number
 */</span>
<span><span>function</span> <span>insert</span><span>(<span>$table</span>,<span>$array</span>)</span>{</span>
    <span>$keys</span>=join(<span>","</span>,array_keys(<span>$array</span>));
    <span>$vals</span>=<span>"'"</span>.join(<span>"','"</span>,array_values(<span>$array</span>)).<span>"'"</span>;
    <span>$sql</span>=<span>"insert {$table}($keys) values({$vals})"</span>;
    mysql_query(<span>$sql</span>);
    <span>return</span> mysql_insert_id();
}
<span>//update imooc_admin set username='king' where id=1</span>
<span>/**
 * 记录的更新操作
 *<span> @param</span> string $table
 *<span> @param</span> array $array
 *<span> @param</span> string $where
 *<span> @return</span> number
 */</span>
<span><span>function</span> <span>update</span><span>(<span>$table</span>,<span>$array</span>,<span>$where</span>=null)</span>{</span>
    <span>foreach</span>(<span>$array</span> <span>as</span> <span>$key</span>=><span>$val</span>){
        <span>if</span>(<span>$str</span>==<span>null</span>){
            <span>$sep</span>=<span>""</span>;
        }<span>else</span>{
            <span>$sep</span>=<span>","</span>;
        }
        <span>$str</span>.=<span>$sep</span>.<span>$key</span>.<span>"='"</span>.<span>$val</span>.<span>"'"</span>;
    }
        <span>$sql</span>=<span>"update {$table} set {$str} "</span>.(<span>$where</span>==<span>null</span>?<span>null</span>:<span>" where "</span>.<span>$where</span>);
        <span>$result</span>=mysql_query(<span>$sql</span>);
        <span>//var_dump($result);</span>
        <span>//var_dump(mysql_affected_rows());exit;</span>
        <span>if</span>(<span>$result</span>){
            <span>return</span> mysql_affected_rows();
        }<span>else</span>{
            <span>return</span> <span>false</span>;
        }
}

<span>/**
 *  删除记录
 *<span> @param</span> string $table
 *<span> @param</span> string $where
 *<span> @return</span> number
 */</span>
<span><span>function</span> <span>delete</span><span>(<span>$table</span>,<span>$where</span>=null)</span>{</span>
    <span>$where</span>=<span>$where</span>==<span>null</span>?<span>null</span>:<span>" where "</span>.<span>$where</span>;
    <span>$sql</span>=<span>"delete from {$table} {$where}"</span>;
    mysql_query(<span>$sql</span>);
    <span>return</span> mysql_affected_rows();
}

<span>/**
 *得到指定一条记录
 *<span> @param</span> string $sql
 *<span> @param</span> string $result_type
 *<span> @return</span> multitype:
 */</span>
<span><span>function</span> <span>fetchOne</span><span>(<span>$sql</span>,<span>$result_type</span>=MYSQL_ASSOC)</span>{</span>
    <span>$result</span>=mysql_query(<span>$sql</span>);
    <span>$row</span>=mysql_fetch_array(<span>$result</span>,<span>$result_type</span>);
    <span>return</span> <span>$row</span>;
}


<span>/**
 * 得到结果集中所有记录 ...
 *<span> @param</span> string $sql
 *<span> @param</span> string $result_type
 *<span> @return</span> multitype:
 */</span>
<span><span>function</span> <span>fetchAll</span><span>(<span>$sql</span>,<span>$result_type</span>=MYSQL_ASSOC)</span>{</span>
    <span>$result</span>=mysql_query(<span>$sql</span>);
    <span>while</span>(@<span>$row</span>=mysql_fetch_array(<span>$result</span>,<span>$result_type</span>)){
        <span>$rows</span>[]=<span>$row</span>;
    }
    <span>return</span> <span>$rows</span>;
}

<span>/**
 * 得到结果集中的记录条数
 *<span> @param</span> unknown_type $sql
 *<span> @return</span> number
 */</span>
<span><span>function</span> <span>getResultNum</span><span>(<span>$sql</span>)</span>{</span>
    <span>$result</span>=mysql_query(<span>$sql</span>);
    <span>return</span> mysql_num_rows(<span>$result</span>);
}

<span>/**
 * 得到上一步插入记录的ID号
 *<span> @return</span> number
 */</span>
<span><span>function</span> <span>getInsertId</span><span>()</span>{</span>
    <span>return</span> mysql_insert_id();
}

<span>?></span></span></span></code>
로그인 후 복사

(5)include.php的制作

<code><span><span><?php </span>
header(<span>"content-type:text/html;charset=utf-8"</span>);
define(<span>"ROOT"</span>,dirname(<span>__FILE__</span>));
<span>//设置包含的路径,之后引入的文件路径都在这里</span>
set_include_path(<span>"."</span>.PATH_SEPARATOR.ROOT.<span>"/lib"</span>.PATH_SEPARATOR.ROOT.<span>"/configs"</span>.PATH_SEPARATOR.ROOT.<span>"/core"</span>.PATH_SEPARATOR);
<span>//引入文件</span>
<span>require_once</span> <span>'mysql.func.php'</span>;
<span>require_once</span> <span>'page.func.php'</span>;
<span>require_once</span> <span>'test.func.php'</span>;
<span>require_once</span> <span>'admin.inc.php'</span>;
<span>require_once</span> <span>'configs.php'</span>;
connect();<span>//连接数据库</span>
<span>?></span></span></span></code>
로그인 후 복사

之后在其他的代码中如果需要以上的部分,再开头加一句
require_once ‘../include.php’;

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿