用php和mysql做登陆注册系统
Posted kinoko-木子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用php和mysql做登陆注册系统相关的知识,希望对你有一定的参考价值。
首先创建数据库:
数据库名:kinoko1824;
创建表(用户表和成绩表):
用户表名:user
主键:uid
设置自增
成绩表名:score
主键:sid
设置自增
在完成之前图解思路:
数据库创建好之后新建register.html,代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="register.php" method = "POST"> <p>用户名:<input type="text" name = "uname"></p> <p>密码:<input type="text" name = "upwd"></p> <p><button>注册</button></p> <div></div> </form> </body> </html> <script> //获取form元素: var oForm = document.forms[0]; //console.log(oForm); //给oForm 添加onsubmit事件: oForm.onsubmit = function(){ //判断如果用户名、密码符合规则,则可以提交,否则不能提交; if(flagname&&flagpwd){ return true; }else{ return false; } } //获取用户名、密码元素: var oName = oForm.elements[0]; var oPwd = oForm.elements[1]; var oDiv = document.querySelector(\'div\'); console.log(oDiv); //声明一个变量控制onsubmit提交: var flagname = null; //添加失焦事件: oName.onblur = function(){ var reg = /^\\w{6,10}/; var str = oForm.elements[0].value; if(reg.test(str)){ flagname = true; }else{ oDiv.innerHTML = "包含字母、数字、下划线,在6-10位之间"; flagname = false; } } //声明一个变量控制onsubmit提交: var flagpwd = null; //添加失焦事件: oPwd.onblur = function(){ var reg = /^\\w{6,10}/; var str = oForm.elements[1].value; if(reg.test(str)){ flagpwd = true; }else{ oDiv.innerHTML = "包含字母、数字、下划线,在6-10位之间"; flagpwd = false; } } </script>
新建register.php,代码如下:
<?php header(\'content-type:text/html;charset=utf-8\'); //接受客户端的数据: $name = $_POST["uname"]; $pwd = $_POST["upwd"]; //操作数据库: //连接数据源: $db = mysql_connect("localhost","root","root"); //选择数据库: mysql_select_db("kinoko1824",$db); //设置编码符: mysql_query("set names utf8"); //编写sql语句: $sql = "INSERT INTO `user`( `uname`, `upwd`) VALUES (\'$name\',\'$pwd\')"; //执行sql语句: $res = mysql_query($sql); //返回数据: if($res){ echo"<script>alert(\'注册成功!\');location.href = \'login.html\';</script>"; }else{ echo"<script>alert(\'注册失败!\');location.href = \'register.html\';</script>"; } ?>
新建login.html,代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="login.php" method = "POST"> <p>用户名:<input type="text" name = "uname"></p> <p>密码:<input type="text" name = "upwd"></p> <p><button>登录</button></p> <div></div> </form> </body> </html>
新建login.php,代码如下:
<?php header(\'content-type:text/html;charset=utf-8\'); //接受客户端的数据: $name = $_POST["uname"]; $pwd = $_POST["upwd"]; //操作数据库: //连接数据源: $db = mysql_connect("localhost","root","root"); //选择数据库: mysql_select_db("kinoko1824",$db); //设置编码符: mysql_query("set names utf8"); //编写sql语句: $sql = "SELECT * FROM `user` WHERE uname = \'$name\'"; //执行sql语句: $res = mysql_query($sql); //将资源转为数组: $arr = mysql_fetch_array($res); //返回数据: if($arr){ if($arr[\'upwd\']==$pwd){ echo"<script>alert(\'登录成功!\');location.href = \'score.php\';</script>"; }else{ echo"<script>alert(\'密码错误!\');location.href = \'login.html\';</script>"; } }else{ echo"<script>alert(\'用户名不存在!\');location.href = \'register.html\';</script>"; } ?>
新建score.php,代码如下:
<?php //引入public.php文件: include \'public.php\'; header(\'content-type:text/html;charset=utf-8\'); echo"<a href = \'add.html\'>添加学生成绩</a>"; //编写sql语句: $sql = "SELECT * FROM `score`"; //执行sql语句: $res = mysql_query($sql); //返回的是一个资源,所以用一个数组来接受这个资源: /* $arr = mysql_fetch_array($res); //把数据库里的数据放到php文件中: echo"<table border = 1 width = 300 cellspacing = 0>"; echo"<tr><th>学号</th><th>姓名</th><th>数学成绩</th><th>语文成绩</th></tr>"; echo"<tr><td>{$arr[\'sid\']}</td><td>{$arr[\'sname\']}</td><td>{$arr[\'math\']}</td><td>{$arr[\'chinese\']}</td></tr>"; echo"</table>"; */ //每执行一次$arr,添加一次数据,因此采用for循环,减少垃圾代码: /* for($id=1;$id<8;$id++){ $arr = mysql_fetch_array($res); //把数据库里的数据放到php文件中: echo"<table border = 1 width = 300 cellspacing = 0>"; echo"<tr><th>学号</th><th>姓名</th><th>数学成绩</th><th>语文成绩</th></tr>"; echo"<tr><td>{$arr[\'sid\']}</td><td>{$arr[\'sname\']}</td><td>{$arr[\'math\']}</td><td>{$arr[\'chinese\']}</td></tr>"; echo"</table>"; } */ //此时$id的值每添加一次就需要改一次,很不灵活,所以使用while循环语句: while($arr=mysql_fetch_array($res)){ //$arr = mysql_fetch_array($res); //把数据库里的数据放到php文件中: echo"<table border = 1 width = 600 cellspacing = 0>"; echo"<tr><th>学号</th><th>姓名</th><th>数学成绩</th><th>语文成绩</th><th>操作</th></tr>"; echo"<tr> <td>{$arr[\'sid\']}</td> <td>{$arr[\'sname\']}</td> <td>{$arr[\'math\']}</td> <td>{$arr[\'chinese\']}</td> <td><a class = \'del\' href = \'javascript: ;\'>删除</a>|<a href = \'update.php ?id={$arr[\'sid\']}\'>修改</a></td> </tr>"; //注意:写id={$arr[\'sid\']}时,等号连接处不能有空格。 echo"</table>"; } ?> <style> td{ text-align:center; } </style> <script> //点击删除有确认提示: //利用事件委托添加点击事件: //获取删除元素以及其父元素table: var oDel = document.getElementsByClassName(\'del\'); var oTable = document.getElementsByTagName(\'table\')[0]; //console.log(oDel); //给oTable添加点击事件: oTable.onclick = function(e){ //做事件兼容: var e=e||event; //委托兼容: var target = e.target||e.srcElement; //判断事件源是否是oDel; if(target.className == \'del\'){ //利用confirm来做确认删除: if(confirm(\'确认要删除吗?\')){ //href = \'delete.php ?id={$arr[\'sid\']}\' //获取当前要删除行对应的id值: var id = target.parentNode.parentNode.children[0].innerHTML; //console.log(id); location.href = "delete.php?id=" + id; } } } </script>
新建add.html,代码如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <form action="add.php" method = "POST"> <input type="hidden" name = "sid"> <p>姓名:<input type="text" name= "sname"></p> <p>语文:<input type="text" name = "schinese"></p> <p>数学:<input type="text" name = "smath"></p> <p><button>添加</button></p> </form> </body> </html>
新建add.php,代码如下:
<?php header(\'content-type:text/html;charset=utf-8\'); //从客户端获取数据: $name = $_POST[\'sname\']; $id = $_POST[\'sid\']; $chinese = $_POST[\'schinese\']; $math = $_POST[\'smath\']; //操作数据库 //连接数据源 $db = mysql_connect(\'localhost\',\'root\',\'root\'); //选择数据库 mysql_select_db(\'kinoko1824\',$db); //设置编码符 mysql_query(\'set names utf8\'); //编写sql语句 $sql = "INSERT INTO `score`(`sid`, `sname`, `chinese`, `math`) VALUES (\'$id\',\'$name\',\'$chinese\',\'$math\')"; //执行sql语句 $res = mysql_query($sql); //判断返回值 if($res){ echo"<script>alert(\'添加成绩成功!\');location.href = \'score.php\';</script>"; }else{ echo"<script>alert(\'添加成绩失败!\');location.href = \'add.html\';</script>"; } ?>
新建delate.php,代码如下:
<?php include"public.php"; //此处的id是通过超链接路径来获取的;用get是因为路径传值(将页面中的数据引入服务器的方法:\'路径?id=$id\') $id = $_GET["id"];//此处的id是?后的key. //编写sql语句: $sql = "DELETE FROM `score` WHERE sid = $id"; //echo"$id"; //执行sql语句: mysql_query($sql); //执行删除语句时的内建代码: $row = mysql_affected_rows(); //判断: if($row){ echo"<script>alert(\'删除成功!\');location.href = \'score.php\';</script>"; }else{ echo"<script>alert(\'删除失败!\');location.href = \'score.php\';</script>"; } ?>
新建update.php,代码如下:
<?php include"public.php"; //获取$id $id = $_GET["id"]; //编写sql语句,在修改之前需要查询表中所有信息: $sql = "SELECT * FROM `score` where sid = $id "; //执行sql语句: $res = mysql_query($sql); //用arr来接收资源: $arr = mysql_fetch_array($res); ?> <form action="updateDo.php" method = "post"> <input type="hidden" name = "sid" value = "<?php echo $arr[\'sid\'];?>"> <p>姓名:<input type="text" name = "sname" value = "<?php echo $arr[\'sname\'];?>"></p> <p>数学成绩:<input type="text" name = "math" value = "<?php echo $arr[\'math\'];?>"></p> <p>语文成绩:<input type="text" name = "chinese" value = "<?php echo $arr[\'chinese\'];?>"></p> <p><button>确认修改</button></p> </form>
新建updateDo.php,代码如下:
<?php include"public.php"; //获取客户端传递的数据: $name = $_POST["sname"]; $id= $_POST["sid"]; $math = $_POST["math"]; $chinese= $_POST["chinese"]; //编写sql语句: $sql = "UPDATE `score` SET `sname`=\'$name\',`chinese`=\'$chinese\',`math`=\'$math\' WHERE sid =$id"; //执行sql语句: mysql_query($sql); //用内建代码来执行: $res = mysql_affected_rows(); //判断: if($res){ echo"<script>alert(\'修改成绩成功!\');location.href = \'score.php\';</script>"; }else{ echo"<script>alert(\'修改成绩失败!\');location.href = \'update.php\';</script>"; } ?>
附件:新建public.php,代码如下:
<?php header(\'content-type:text/html;charset=utf-8\'); //操作数据库: //连接数据源: $db = mysql_connect("localhost","root","root"); //选择数据库: mysql_select_db("kinoko1824",$db); //设置编码符: mysql_query("set names utf8");
以上是关于用php和mysql做登陆注册系统的主要内容,如果未能解决你的问题,请参考以下文章