php+mysql实现基本网站数据库管理功能
1、本文以非常简洁的代码实现网站数据库的基本功能:增、删、查、改,对于网站建设初学者来说非常容易接受和理解。网站效果如下:

2、整个网站包括6个php文件,分别是conn.php,index.php,menu.php,add.php,action.php,edit.php。此外还包括1个样式文件和1个mysql数据库文件。网站环境是php+mysql,mysql数据库文件名是stumanage,其中数据表文件名是student。表student的结构如下:

3、下面说明网站中的各个文件:
数据库连接文件conn.php的代码如下:
<?php
$server="localhost";//服务器主机
$db_username="root";//用户名
$db_password="root";//密码
$con = mysql_connect($server,$db_username,$db_password);//mysql_connect()链接数据库函数
if(!$con){
die("不能连接".mysql_error());//如果链接失败输出错误。die() 函数输出一条消息,并退出当前脚本。
//mysql_error() 函数返回上一个 MySQL 操作产生的文本错误信息。
}
mysql_select_db('stumanage',$con);//选择数据库(当前的是test1)
//mysql_select_db() 函数设置活动的 MySQL 数据库。
mysql_query("set names utf8");
?>
4、首页文件index.php的代码如下:
!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>学生信息管理</title>
</head>
<body>
<center>
<?php
include('menu.php');
?>
<h3>浏览学生信息</h3>
<table width="500" border="1" cellspacing="0" cellpadding="5">
<tr>
<th>id</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>班级</th>
<th>操作</th>
</tr>
<?php
include('conn.php');
$q1="select * from student";
$result=mysql_query($q1,$con) or die("查询失败".mysql_error());
while($row=mysql_fetch_array($result)){
echo "<tr style='text-align:center;'>";
echo "<td>{$row['id']}</td>";
echo "<td>{$row['name']}</td>";
echo "<td>{$row['sex']}</td>";
echo "<td>{$row['age']}</td>";
echo "<td>{$row['classname']}</td>";
echo "<td>
<a href='edit.php?id={$row['id']}'>修改</a>
<a href='javascript:void(0);' onclick='doDel({$row['id']})'>删除</a>
</td>";
echo "</tr>";
}
mysql_close($con);
?>
</table>
</center>
<script>
function doDel(id){
if(confirm('确认删除?')){
window.location='action.php?action=del&id='+id;
}
}
</script>
</body>
</html>
5、主导航文件menu.php的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" type="text/css" href="css/stu.css"/>
</head>
<body>
<h2>学生管理系统</h2>
<div class="dh">
<a href="index.php">浏览学生</a>
<a href="add.php">添加学生</a>
</div>
</body>
</html>
6、外部样式文件stu.css的代码如下:
body{
font-size:14px;
}
a{
color:#333;
text-decoration: none;
}
a:hover{
color:#f00;
}
.dh{
background:#333;
width:50%;
}
.dh a{
font-size:16px;
color:#fff;
height:40px;
display:inline-block;
line-height: 40px;
width:100px;
}
.dh a:hover{
background:#f00;
}
7、增加记录文件add.php的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理系统</title>
</head>
<body>
<center>
<?php
include('menu.php');
?>
<h3>增加学生信息</h3>
<form action="action.php?action=add" method="post">
<table>
<tr>
<td>姓名:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="sex" value="男">男</td>
<td><input type="radio" name="sex" value="女">女</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>班级:</td>
<td><input type="text" name="classname"></td>
</tr>
<tr>
<td><input type="submit" value="添加"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
8、功能实现文件action.php的代码如下:
<meta charset="utf-8">
<?php
include('conn.php');
switch($_GET['action']){
case 'add':
$name=$_POST['name'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$classname=$_POST['classname'];
$s1="insert into student(name,sex,age,classname) values('{$name}','{$sex}',{$age},'{$classname}')";
$r1=mysql_query($s1,$con);
if(!$r1){
die('添加失败'.mysql_error());
}else{
echo "添加成功";
}
echo "<script>";
echo "window.location.href ='index.php'";
echo "</script>";
break;
case 'del':
//get方式获取
$id=$_GET['id'];
$s2="delete from student where id={$id}";
$r2=mysql_query($s2,$con);
if(!$r2){
die('删除失败'.mysql_error());
}else{
echo "删除成功";
}
echo "<script>";
echo "window.location.href ='index.php'";
echo "</script>";
break;
case 'edit':
//post方式获取
$id=$_POST['id'];
$name=$_POST['name'];
$sex=$_POST['sex'];
$age=$_POST['age'];
$classname=$_POST['classname'];
mysql_query("set names utf8");
$s3="update student set name='{$name}',sex='{$sex}',age={$age},classname='{$classname}' where id={$id}";
$r3=mysql_query($s3,$con);
if(!$r3){
die('修改失败'.mysql_error());
}else{
echo "修改成功";
}
echo "<script>";
echo "window.location.href ='index.php'";
echo "</script>";
break;
default:
echo "<script>";
echo "window.location.href ='#'";
echo "</script>";
break;
}
?>
9、修改记录文件edit.php的代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>学生管理系统</title>
</head>
<body>
<center>
<?php
include('menu.php');
include('conn.php');
$sql_edit="select * from student where id={$_GET['id']}";
$s_edit=mysql_query($sql_edit,$con);
$row=mysql_fetch_array($s_edit);
/*if(row_count()>0){
$row=mysql_fetch_array($s_edit);
}else{
die("没有这个id:{$_GET['id]}");
}*/
?>
<h3>修改学生信息</h3>
<form action="action.php?action=edit" method="post">
<input type="hidden" name="id" value="<?php echo $row['id'];?>"/>
<table>
<tr>
<td>姓名</td>
<td><input type="text" name="name" value="<?php echo $row['name']; ?>"></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="sex" value="男" <?php echo $row['sex']=='男'?'checked':''; ?> >男</td>
<td><input type="radio" name="sex" value="女" <?php echo $row['sex']=='女'?'checked':''; ?> >女</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age" value="<?php echo $row['age'];?>"></td>
</tr>
<tr>
<td>班级</td>
<td><input type="text" name="classname" value="<?php echo $row['classname'];?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="确认修改"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>
至此,案例制作完成。本项目案例的特点在于代码精简、好理解,希望对大家的学习与进步有非常大的帮助。大家觉得好的话一定记得点赞与分享呀!