赞
踩
单独将数据库的配置文件提取出来
common.php
- <!-- 连接完成 -->
-
- <?php
- $servername = "localhost";
- $username = "root"; // 用户名
- $password = "3z2Myca2LHXriWLF"; // 密码
- $dbname = "app_aptus_xin"; // 对应数据库
-
- // 创建连接
- $con = new mysqli($servername,$username,$password,$dbname);
-
- if ($con->connect_error) {
- die("连接失败:" . $con->connect_error);
- }
index.html代码
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>一个简单的项目</title>
- </head>
- <body>
- <h2>管理系统</h2>
- <h2>用户管理</h2>
- <a href="">添加用户</a><br/>
- <a href="">查看用户</a><br/>
- <a href="">删除用户</a><br/>
- <a href="">修改用户</a>
- </body>
- </html>
运行结果:
- <!--已完成-->
-
- <?php require_once "../Model/common.php";?>
- <html>
- <head>
- <title>添加用户</title>
- </head>
- <body>
- <h3>添加用户</h3>
- <form id="add_user" name="add_user" method="post" action="insert_user.php">
- 用户姓名:<input type="text" name="user_name"/><br/>
- 用户密码:<input type="text" name="user_psw"/><br/>
- 用户性别:<input type="text" name="user_sex"/><br/>
- 用户年龄:<input type="text" name="user_age"/><br/>
- 所属班级:<select name="show_user_name">
-
- <?php
- $sql="select * from dept";
- $result=mysqli_query($con,$sql);
- while($rows=mysqli_fetch_row($result)){
- echo "<option value=".$rows[0].">".$rows[1]."</option>";
- }
- ?>
- </select><br/>
-
- 小组名:<select name="user_group">
- <?php
- $sql="select * from usergroup";
- $result=mysqli_query($con,$sql);
- while($rows=mysqli_fetch_row($result)){
- echo "<option value=".$rows[0].">".$rows[1]."</option>";
- }
- ?>
- </select><br/>
- <br/>
- <input type="submit" value="添加"/>
- </form>
- </body>
- </html>
这时我们要对填写的表单进行操作,写入mysql对应的表里面,这个操作我单独写在insert_user里面了
- <!--已完成-->
- <?php require_once "../Model/common.php";?>
-
-
- <?php
- if(empty($_POST['user_name'])||empty($_POST['user_psw'])
- ||empty($_POST['user_sex'])||empty($_POST['user_age']))
- {
- die("请将信息填写完整!");
- }
- else
- {
- $UserName=($_POST['user_name']);
- //echo $UserName;
- $UserPsw =($_POST['user_psw']);
- $UserSex =($_POST['user_sex']);
- $UserAge =($_POST['user_age']);
- $UserCls =($_POST['show_user_name']);
- $UserGrp =($_POST['user_group']);
- // echo $UserName."<br>";
- // echo $UserPsw."<br>";
- // echo $UserSex."<br>";
- // echo $UserCls."<br>";
- // echo $UserGrp."<br>";
-
- $sql = "insert into users(user_name, user_psw, user_sex,user_age,user_dept,
- user_group)values('$UserName','$UserPsw','$UserSex','$UserAge',
- '$UserCls','$UserGrp')" ;
-
- //插入数据库
- if (!(mysqli_query( $con , $sql )))
- {
- echo "<script>alert('数据插入失败');//window.location.href='../'</script>" ;
-
- }
- else
- {
- echo "<script>alert('注册成功!')</script>" ;
-
- $q="select * from users where user_name='".$UserName."'";//设置查询指令
- $result=mysqli_query($con,$q);//执行查询
- $row=mysqli_fetch_assoc($result);
-
- echo "用户编号:".$row['user_id']."<br>";
- echo "用户姓名:".$UserName."<br>";
- echo "用户密码:".$UserPsw."<br>";
- echo "用户性别:".$UserSex."<br>";
- echo "用户年龄:".$UserAge."<br>";
- echo "用户班级:".$UserCls."<br>";
- echo "用户小组:".$UserGrp."<br>";
-
- }
-
-
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。