自己做的一个简单的用户管理(PHP)!

这是一个用于课程设计的PHP用户管理系统,包括数据库设计、用户查询、添加、修改和删除功能。使用MySQL作为数据库,通过PHP连接并操作数据。用户可以通过ID、用户名、年龄或性别进行查询,系统会自动跳转到相应页面完成操作,并在5秒后返回主页。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  自己为了老师的课程设计做的小东西。
1)。数据库(MYSQL):
    use test;
    create table if not exists user(
    id int auto_increment not null,
    username varchar(20) not null,
    age int  not null,
    sex char(1) not null);
    数据库连接(test3con.php):
    <?php
    $link=mysql_connect("localhost","root","123");
    if(!link) die ("Could not connect to MySQL");
    mysql_select_db("test",$link) or die ("Could not open db".mysql_error());
    ?>
   
    主页为(test3.php):
    <?php
    include("test3con.php");
    ?>
    <center>
    用户管理系统
    <hr>
    <?php
        $query_count="select count(*) from user";
        $result_count=mysql_query($query_count,$link);
        $count=mysql_result($result_count,0);
    ?>
    <table>
    <tr>
    <td align="right">
    总的用户数:<font color="red"><?php echo $count; ?></font>|<a href="add.php">添加新用户</a>
    </td>
    <tr><td>
        <table>
        <th align="right">ID</th><th align="right">姓名</th><th align="right">年龄</th><th align="right">性别    </th><th align="right">修改</th><th align="right">删除</th>
        <?php
            $query="select * from user order by id";
            $result=mysql_query($query,$link);
            while($a_rows=mysql_fetch_object($result))
            {
        ?>
                <tr>
                <td align="right" width="100"><?php echo $a_rows->id ?></td>
                <td align="right" width="100"><?php echo $a_rows->username ?></td>
                <td align="right" width="100"><?php echo $a_rows->age ?></td>
                <td align="right" width="100"><?php echo $a_rows->sex ?></td>
                <td align="right" width="100"><a href="modify.php?id=<?php echo $a_rows->id ?>">修改</a></td>
                <td align="right" width="100"><a href="delete.php?id=<?php echo $a_rows->id ?>">删除</a></td>
                </tr>               
        <?php
            }
                mysql_close($link);
        ?>
        <tr><br>
            <td align="right" colspan="6"><br>
            <form action="search.php" method="post">
                指定查询统计:
                    <select name="select">
                        <option value="id" selected>ID</option>
                        <option value="username">用户名</option>
                        <option value="age">年龄</option>
                        <option value="sex">性别</option>
                    </select>
                    值:<input type="text" name="values">
                    <input type="submit" value="查询统计">
            </form>
        </td>       
    </tr>   
    </table>
    </td>
    </tr>
    </table>
    </center>
3)添加新的用户(add.php):
<?php
include("test3con.php");
$temp_id;
$query_count="select count(*) from user";
$result_count=mysql_query($query_count,$link);
$count=mysql_result($result_count,0);
?>
<center>
添加新用户
<hr>
<table>
<tr>
<form action="add_action.php" method="post">
<td>ID:</td><td><input type="text" name="id" value="<?php echo $count ?>"></td></tr>
<td>用户名:</td><td><input type="text" name="username"></td></tr>
<tr><td>年龄:</td><td><input type="text" name="age"></td></tr>
<tr><td>性别:</td><td><select name="sex">
                <option value="F">Female</option>
                <option value="M">Male</option>
            </select></td></tr>
<tr><td><input type="submit" value="添加"></td><td><input type="reset"></td></tr>
</form>
</table>
</center>
<?php
mysql_close($link);
?>
添加用户的处理(add_action.php):
<HEAD>
<META HTTP-EQUIV="REFRESH" CONTENT="5;URL=test3.php">
</HEAD>
<?php
include("test3con.php");
$add_id=intval($_POST['id']);
$add_username=$_POST['username'];
$add_age=intval($_POST['age']);
$add_sex=$_POST['sex'];
$flag=true;
$check="select * from user";
$check_result=mysql_query($check,$link);
while($result_rows=mysql_fetch_object($check_result))
{
    if(strcmp($add_usernme,$result_rows->username)!=0 && $result_rows!=$add_id)
        $flag=false;
    else
        $flag=true;
}
    if($flag==false)
    {
        $temp_age=intval($add_age);
        if($temp_age>0 && $temp_age<150)
        {
            $sql="insert into user(id,username,age,sex) values('$add_id','$add_username','$add_age','$add_sex')";
            if(!mysql_query($sql,$link)){
             die("Could not add new user.".mysql_error());
            }else
            {
                echo "操作成功!五秒钟自动返回主页.<br><a href=/"test3.php/">点击返回主页</a>";
            }
        }
        else
            echo "年龄超出范围!<br><a href=/"add.php/">返回</a>";
    }
    else{
        echo "该用户已经存在!<a href=/"add.php/">返回</a>";
    }
mysql_close($link);
?>
4)修改用户信息(modify.php):
<?php
    include("test3con.php");
   
    $m_id=intval($_GET['id']);
    $query="select username,age,sex from user where id=$m_id";
    $result=mysql_query($query,$link);
    while($m_rows=mysql_fetch_object($result))
    {
        $m_username=$m_rows->username;
        $m_age=$m_rows->age;
        $m_sex=$m_rows->sex;
?>
<center>
修改用户数据
<hr>
<form action="modify_action.php" method="post">
<input type="hidden" name="id" value="<?php echo $m_id ?>">
<table>
<tr>
    <td>用户名:</td><td><input type="text" name="username" value="<?php echo $m_username ?>"></td>
</tr>
<tr>
    <td>年龄:</td><td><input type="text" name="age" value="<?php echo $m_age ?>"></td>
</tr>
<tr>
    <td>性别:</td><td><select name="sex">
                <?php
                    if(strcmp($m_sex,"F")==0){
                        echo "<option value=/"F/" selected>Female</option>";
                        echo "<option value=/"M/">Male</option>";
                    }else{
                    echo "<option value=/"F/">Female</option>";
                      echo "<option value=/"M/" selected>Male</option>";
                }?>
            </select>
    </td>
</tr>
<tr><td><input type="submit" value="修改"></td><td><input type="reset"></td></tr>
</form>
<?php
}
mysql_close($link);
?>
</table>
<br>
<a href="test3.php">点击返回主页</a>
</center>
修改用户信息的处理(modify_action.php):
<HEAD>
<META HTTP-EQUIV="REFRESH" CONTENT="5;URL=test3.php">
</HEAD>

<?php
include("test3con.php");
$ma_id=intval($_POST['id']);
$ma_username=$_POST['username'];
$ma_age=intval($_POST['age']);
$ma_sex=$_POST['sex'];
$flag=true;

$check="select * from user";
$check_result=mysql_query($check,$link);
while($result_rows=mysql_fetch_object($check_result))
{
    if(strcmp($ma_usernme,$result_rows->username)!=0)
        $flag=false;
    else
        $flag=true;
}
    if($flag==false)
    {
        $temp_age=intval($ma_age);
        if($temp_age>0 && $temp_age<150)
        {
            $sql="update user set username='$ma_username',age='$ma_age',sex='$ma_sex' where id=$ma_id";
            if(!mysql_query($sql,$link))
             die("Could not update the data!".mysql_error());
            else
            {
                echo "操作成功!五秒钟自动返回主页.<br><a href=/"test3.php/">点击返回主页</a>";
            }
        }
        else
            echo "年龄超出范围!<br><a href=/"modify.php/">返回</a>";
    }
    else{
        echo "该用户已经存在!<a href=/"modify.php/">返回</a>";
    }
mysql_close($link);
?>
5)删除用户信息的处理(delete.php):
<HEAD>
<META HTTP-EQUIV="REFRESH" CONTENT="5;URL=test3.php">
</HEAD>
<?php
    include("test3con.php");   
    $d_id=intval($_GET['id']);
    $sql="delete from user where id=$d_id";
    if(!mysql_query($sql,$link))
    {
        die("Could not delete the data".mysql_error());
    }
    else
{
    echo "操作成功!五秒钟自动返回主页.<br>";
}
mysql_close($link);
?>
<a href="test3.php">点击返回主页</a>
6)查询用户信息处理(search.php):
<HEAD>
<META HTTP-EQUIV="REFRESH" CONTENT="5;URL=test3.php">
</HEAD>
<center>
查询结果
<hr>
<table>
<?php
include("test3con.php");
$search_name=$_POST['select'];
$search_value=$_POST['values'];

$sql="select * from user";
$sql_sum="select count(id) from user";

if(strcmp($search_name,"id")==0)
{
    $sql=$sql." where id=$search_value";
    $sql_sum=$sql_sum." where id=$search_value";
}
if(strcmp($search_name,"username")==0)
{
    $sql=$sql." where username='$search_value'";
    $sql_sum=$sql_sum." where username='$search_value'";
}
if(strcmp($search_name,"age")==0)
{
    $sql=$sql." where age=$search_value";
    $sql_sum=$sql_sum." where age=$search_value";
}
if(strcmp($search_name,"sex")==0)
{
    $sql=$sql." where sex='$search_value'";
    $sql_sum=$sql_sum." where sex='$search_value'";
}
$sql=$sql." order by id";

$result=mysql_query($sql,$link);

$result_sum=mysql_query($sql_sum,$link);
$count_sum=mysql_result($result_sum,0);
echo"<tr align=/"right/"><td align=/"right/">符合要求的总数:".$count_sum."</td></tr>";
?>
<tr><td>
    <table><br>
    <th align="right">ID</th><th align="right">姓名</th><th align="right">年龄</th><th align="right">性别</th>
    <?php
    if(!$result){
        echo "没有查找到你的数据!";
    }else{
        while($a_rows=mysql_fetch_object($result))
        {
            echo"<tr><td align=/"right/" width=/"100/">".$a_rows->id."</td>";
            echo"<td align=/"right/" width=/"100/">".$a_rows->username."</td>";
            echo"<td align=/"right/" width=/"100/">".$a_rows->age."</td>";
            echo"<td align=/"right/" width=/"100/">".$a_rows->sex."</td></tr>";       
        }
    }
   
    mysql_close($link);
    ?>
    </table>
</td>
</tr></table>   
<br>五秒钟自动返回主页!
<a href="test3.php">点击返回主页>></a>;
</center>
整个工作就完成了。功能比较简单,但是五脏比较齐全。
还请各位大虾指正。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值