赞
踩
-----前言-----
(可以帮忙接一些简单的答疑指导和简单的php课程设计,直接私信就好)
非常小白,大佬忽略。
之前看见的一个博客没写全的项目,就把它粘下来改了改补全了(至少增删改查这些基础数据库操作加上了),分了一下mvc,因为我只是刚学php的小白,MVC不是很明确,model还写不好,于是处理mysql的都单独写在了controller里面,导致controller有些多,就只是入门的php。
在这里我只放用户的增删改查。(小组和班级都是依葫芦画瓢)
github自提地址:GitHub - lybbor/simple_user_mannage: A very simple user management system.
-----------------
目录
数据库:
链接数据库操作,单独写出来就可以不用每次单独写代码来实现链接数据库了:
common.php代码:
- <?php
- $servername = "localhost";
- $username = "root"; //用户名
- $password = "root"; //密码
- $dbname = "db_user"; //对应的数据库
-
- // 创建连接
- $con = new mysqli($servername, $username, $password, $dbname);
-
- if ($con->connect_error) {
- die("连接失败: " . $con->connect_error);
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>我会个der?</title>
- </head>
- <body>
- <h2>管理系统</h2>
- <h3>用户管理</h3>
- <a href="Controller/add_user.php">添加用户</a><br/> <!---已完成-->
- <a href="Controller/show_user.php">查看用户</a><br/> <!---已完成-->
- <a href="Controller/delete_user.php">删除用户</a><br/> <!---已完成-->
- <a href="Controller/change_user.php">修改用户</a>
-
- <h3>班级管理</h3>
- <a href="Controller/add_dept.php">添加班级</a><br/> <!---已完成-->
- <a href="Controller/show_dept.php">查看班级</a> <!---已完成-->
-
- <h3>小组管理</h3>
- <a href="Controller/add_usergroup.php">添加小组</a><br/> <!---已完成-->
- <a href="Controller/show_usergroup.php">查看小组</a> <!---已完成-->
-
-
-
- <!-- <h3>小组权限管理</h3>
- <a href="Controller/add_fun.php">添加权限</a><br/>
- <a href="Controller/show_fun.php">查看权限</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>";
-
- }
-
- }
-
- <?php require_once "../Model/common.php";?>
- <html>
- <head><title>查看用户</title>
- </head>
- <body>
- <h3>查看用户</h3>
- <form id="show_user" name="show_user" method="post" action="select_user.php">
- 用户姓名:<input type="text" name="show_user_name"/><br/>
- 所属班级:<select name="show_user_dept">
- <option value=0>所有班级</option>
-
- <?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/>
- <br/>
- <input type="submit" value="查看"/>
- </form>
- </body>
- </html>
- <?php require_once "../Model/common.php";?>
- <?php
- if(empty($_POST['show_user_name'])){
-
- die("请输入用户姓名!");
- }
-
- $get_user_name1= ($_POST['show_user_name']);
- /*$get_user_name1= empty($_POST['show_user_name'])? die("请输入用户姓名!"):
- mysqli_escape_string($_POST['show_user_name']) ;*/
- //echo "$get_user_name1 <br>";
-
- $get_user_dept1=($_POST['show_user_dept']);
-
-
- $sql="SELECT * FROM users
- WHERE user_name='"."$get_user_name1"."'"." AND user_dept='"."$get_user_dept1"."'";
- $tem=mysqli_query($con,$sql);
- if(!($tem))
- {
- die('数据读取出错,请重新查询!');
- }
- else
- {
- if(! $row=mysqli_fetch_array($tem))
- {
- echo "<script>alert('无此用户!')</script>";
- }
- else{
- echo ("<script>alert('查询成功!')</script>");
- do
- {
- echo "用户编号为:"."{$row['user_id']} <br>";
- echo "用户名字为:"."{$row['user_name']} <br>";
- echo "用户密码为:"."{$row['user_psw']} <br>";
- echo "用户性别为:"."{$row['user_sex']} <br>";
- echo "用户年龄为:"."{$row['user_age']} <br>";
- echo "用户班级为:"."{$row['user_dept']} <br>";
- echo "用户小组为:"."{$row['user_group']} <br><br>";
- }while($row=mysqli_fetch_array($tem));
- }
- }
(包含了Mysql处理,这次的mysql操作和表单我放在一起了)
- <?php require_once "../Model/common.php";?>
-
- <head><title>删除用户</title>
- </head>
- <form action="" method="post">
- 输入需要删除的用户编号:<input type="text" name="delete_id" />
- <input type="submit" name="submit" value="确定">
- </form>
-
- <?php
- //验证---
- //$sql="select * from users";
- //$result=mysqli_query($con,$sql);
- //while($rows=mysqli_fetch_row($result)){
- //echo "$rows[0] <br>";
- //}
- //------
-
-
- if(empty($_POST['delete_id'])){
- die("请输入需要删除的用户编号!");
- }
-
-
- $the_delete_user_id=($_POST['delete_id']);
- echo "$the_delete_user_id";
- $sql="DELETE FROM users WHERE user_id=";
- $sql=$sql.$the_delete_user_id;
-
- //echo $sql;
- //var_dump(mysqli_query($con,$sql));
-
- ///有问题部分
- if(mysqli_query($con,$sql))
- echo "用户删除成功!<br>";
- else
- echo "用户删除失败!<br>";
- ?>
- <?php require_once "../Model/common.php";?>
- <html>
- <head><title>修改用户</title>
- </head>
- <body>
- <h3>修改用户</h3>
- <form id="add_user" name="add_user" method="post" action="update_user.php" >
- 需要修改的用户的id:<input type="text" name="the_user_id"/><br/>
- <!-- ?user_id=
- ?php echo trim($_GET['user_id']);?>
- -->
- <br/>
- 用户姓名:<input type="text" name="the_user_name"/><br/>
- 用户口令:<input type="text" name="the_user_psw"/><br/>
- 用户性别:<input type="text" name="the_user_sex"/><br/>
- 用户年龄:<input type="text" name="the_user_age"/><br/>
- 所属班级:<select name="the_user_dept">
- <option value=0>请选择班级</option>
- <?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="the_user_group">
- <option value="0">请选择小组</option>
- <?php
- $sql="select * from usergroup";
- $res=mysqli_query($con,$sql);
- while($rows=mysqli_fetch_row($res)) {
- echo "<option value=".$rows[0].">".$rows[1]."</option>";
- }
-
- ?>
- </select><br/>
- <br/>
- <input type="submit" value="修改用户信息"/>
- </form>
- </body>
- </html>
- <?php require_once "../Model/common.php";?>
-
- <?php
-
- $get_user_id=($_POST["the_user_id"]);
- $get_user_name=($_POST['the_user_name']);
- $get_user_psw=($_POST['the_user_psw']);
- $get_user_sex=($_POST['the_user_sex']);
- $get_user_age=($_POST['the_user_age']);
- $get_user_dept=($_POST['the_user_dept']);
- $get_user_group=($_POST['the_user_group']);
-
-
- echo "$get_user_name <br/>";
-
- $sql="UPDATE users SET user_name='".
- $get_user_name."',user_psw='".$get_user_psw."',user_sex='".
- $get_user_sex."',user_age='".$get_user_age."',user_dept='".
- $get_user_dept."',user_group='".$get_user_group."'".
- " WHERE user_id='";
- $sql=$sql.$get_user_id;
-
-
-
-
- $sql="UPDATE users SET user_name='".
- $get_user_name."',user_psw='".$get_user_psw."',user_sex='".
- $get_user_sex."',user_age='".$get_user_age."',user_dept='".
- $get_user_dept."',user_group='".$get_user_group."'".
- " WHERE user_id=";
- $sql=$sql.$get_user_id.";";
- // echo $get_user_group;
- // echo $sql;
-
- if(mysqli_query($con,$sql))
- echo "用户修改成功!<br>";
- else
- echo "用户修改失败!<br>";
非常简单,几乎等于没设计 :P
跟原帖子的数据库一样,我没有改动数据库,主要是学习php和mysql相关命令,表的结构我就没细想了
最后,只要数据库没问题,链接没问题,就可以实现了,可以自己写一下关于班级和小组的增删改查,我是通过自己修改和增加原博主,自己动手写的话学习得更快,加油鸭大家。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。