数据库源代码
CREATE TABLE students(
id int not null AUTO_INCREMENT PRIMARY KEY,
major char(10) NOT null,
class char(20) NOT null,
student_id char(20) NOT null,
name char(20) NOT null,
grade char(20) NOT null,
birthday char(20) NOT null
)DEFAULT CHARSET=UTF8##
数据库数据(方便操作)
INSERT INTO students(major,class,student_id,name,grade,birthday) VALUES
('统计学','J21008','J2100808','李瑶','女','2003-03-05'),
('物理学','F21009','F2100939','王三','男','2001-07-15'),
('计算机','J21015','J2101566','张珂','女','2002-01-04'),
('英语','F21001','F2100157','林健','男','2000-12-02'),
('数学','S21003','S2100347','黄浩','男','2003-05-26'),
('地球科学','H20001','H2000167','冯楠','女','1999-10-18'),
('生物科学','S19006','S1900645','李刚','男','2001-11-30'),
('化学','F21004','F2100451','周玉','女','2002-06-19'),
('心理学','J20002','J2000279','吴娜','女','1999-02-28'),
('文学','F21005','F2100553','朱江','男','2004-04-11'),
('物理学','F21008','F2100839','邵菲','女','2002-09-22'),
('计算机','J21013','J2101374','胡阁','男','2003-08-19'),
('地球科学','H19003', 'H1900348','刘维','男','2000-03-07')
主页
//数据库操作.php
<style>
table{
border: 1px solid black;
width: 700px;
text-align: center;
margin:0 auto;
}
th{
background-color: aqua;
border: 1px solid black;
}
td{
border: 1px solid black;
}
</style>
<form action="数据库搜索.php" method="post">
<table>
<tr><td colspan="8">请选择你要查询的字段
<select name="ziduan">
<option value="id">id</option>
<option value="major">major</option>
<option value="class">class</option>
<option value="student_id">student_id</option>
<option value="name">name</option>
<option value="birthday">birthday</option>
</select>
请输入你要查询的内容
<input type="text" name="neirong">
<input type="submit" value="搜索" name="selsub"">
</td></tr></table>
</form>
<table>
<form action="#" method="post">
<tr><th>ID</th>
<th>专业</th><th>班级</th><th>学号</th><th>姓名</th><th>性别</th><th>出生日期</th><th>操作</th></tr>
//连接数据库
<?php
$link = mysqli_connect('127.0.0.1','root','','test','3306');
echo $link?'':'数据库连接失败';
mysqli_query($link,"set names utf8");
$query = "select * from students";
$result = mysqli_query($link,$query)or die("执行语句失败");
$count = 0;
while($arr = mysqli_fetch_assoc($result)){
$count++;
$alt = ($count % 2)?"alt":"";
echo"<tr class={$alt}><td>{$arr['id']}</td><td>{$arr['major']}</td>".
"<td>{$arr['class']}</td><td>{$arr['student_id']}</td>".
"<td>{$arr['name']}</td><td>{$arr['grade']}</td><td>{$arr['birthday']}</td>".
"<td><a href=\"数据库修改.php?id={$arr['id']}\">编辑</a> <a href=\"数据库删除.php?id={$arr['id']}\">删除</a></td></tr>";
}
mysqli_free_result($result);
mysqli_close($link);
?>
</form>
</table>
<center><a href="数据库编辑.php">添加数据</a></center>
对单个数据进行编辑
//数据库修改.php
<?php
$link = mysqli_connect('127.0.0.1','root','','test','3306');
echo $link?'':'数据库连接失败';
mysqli_query($link,"set names utf8");
$result = mysqli_query($link,"select * from students where id={$_GET['id']}");
$arr = mysqli_fetch_assoc($result);
?>
<style>
.aaa{
width: 500px;
margin: 0 auto;
border: 1px solid black;
}
table{
margin: 0 auto;
}
h1{
text-align: center;
}
td{
padding-bottom: 5px;
}
input[type=submit],input[type=reset]{
background: blue;
}
</style>
<div class="aaa">
<h1>编辑学生信息</h1>
<table>
<form action="数据库提交.php" method="post">
<tr><td>学号:</td><td><input type="text" name="student_id" value="<?php echo $arr['student_id'] ?>"></td></tr>
<tr><td>姓名:</td><td><input type="text" name="username" value="<?php echo $arr['name'] ?>"></td></tr>
<tr><td>性别:</td><td><input type="radio" name="xingbie" value="男" <?php if($arr['grade']=='男')echo "checked";?>>男
<input type="radio" name="xingbie" value="女" <?php if($arr['grade']=='女')echo "checked";?>>女</td></tr>
<tr><td>出生日期:</td><td><input type="text" name="birthday" value="<?php echo $arr['birthday'] ?>"></td></tr>
<tr><td>班级:</td><td><input type="text" name="class" value="<?php echo $arr['class'] ?>"></td></tr>
<tr><td>专业:</td><td><input type="text" name="major" value="<?php echo $arr['major'] ?>"></td></tr>
<tr><td><input type="hidden" name="id" value="<?php echo $arr['id'] ?>"></td></tr>
<tr><td><input type="submit" value="保存信息" name="submit"></td><td><input type="reset" value="重新填写"></td></tr>
</form>
</table>
</div>
修改后的数据进行提交
<?php
$link = mysqli_connect('127.0.0.1','root','','test','3306');
echo $link?'数据库连接成功':'数据库连接失败';
mysqli_query($link,"set names utf8");
$id = $_POST['id'];
$student_id = $_POST['student_id'];
$username = $_POST['username'];
$xingbie = $_POST['xingbie'];
$birthday = $_POST['birthday'];
$class = $_POST['class'];
$major = $_POST['major'];
$query = "update students set major='{$major}',class='{$class}',student_id='{$student_id}',name='{$username}',grade='{$xingbie}',birthday='{$birthday}' where id={$id}";
$result = mysqli_query($link,$query);
if($result){
echo "<script>alert('修改成功');window.location.href='数据库操作.php'</script>";
}
else{
echo "<script>alert('修改失败');window.location.href='数据库操作.php'</script>";
}
?>
对数据进行删除
//数据库删除.php
<?php
$link = mysqli_connect('127.0.0.1','root','','test','3306');
echo $link?'':'数据库连接失败';
mysqli_query($link,"set names utf8") or die("语句执行失败");
$id = $_GET['id'];
$query = "delete from students where id={$id}";
$result = mysqli_query($link,$query);
//返回
if($result){
echo "<script>alert('删除成功');window.location.href='数据库操作.php'</script>";
}
else{
echo "<script>alert('删除失败');window.location.href='数据库操作.php'</script>";
}
?>
添加新数据
//数据库编辑.php
<style>
.aaa{
width: 500px;
margin: 0 auto;
border: 1px solid black;
}
table{
margin: 0 auto;
}
h1{
text-align: center;
}
td{
padding-bottom: 5px;
}
input[type=submit],input[type=reset]{
background: blue;
}
</style>
<div class="aaa">
<h1>编辑学生信息</h1>
<table>
<form action="#" method="post">
<tr><td>学号:</td><td><input type="text" name="student_id"></td></tr>
<tr><td>姓名:</td><td><input type="text" name="username"></td></tr>
<tr><td>性别:</td><td><input type="radio" name="xingbie" value="男">男
<input type="radio" name="xingbie" value="女">女</td></tr>
<tr><td>出生日期:</td><td><input type="text" name="birthday"></td></tr>
<tr><td>班级:</td><td><input type="text" name="class"></td></tr>
<tr><td>专业:</td><td><input type="text" name="major"></td></tr>
<tr><td><input type="submit" value="保存信息" name="submit"></td><td><input type="reset" value="重新填写"></td></tr>
</form>
</table>
</div>
<?php
$link = mysqli_connect('127.0.0.1','root','','test','3306');
echo $link?'':'数据库连接失败';
mysqli_query($link,"set names utf8");
if(isset($_POST['submit'])){
$student_id = $_POST['student_id'];
$username = $_POST['username'];
$xingbie = $_POST['xingbie'];
$birthday = $_POST['birthday'];
$class = $_POST['class'];
$major = $_POST['major'];
if(!empty($student_id)&&!empty($username)&&!empty($xingbie)&&!empty($birthday)&&!empty($class)&&!empty($major)){
$query = "insert into students(major,class,student_id,name,grade,birthday)".
"values('$major','$class','$student_id','$username','$xingbie','$birthday')";
mysqli_query($link,$query) or die("执行语句失败");
echo "<script>alert('保存成功');window.location.href='数据库操作.php'</script>";
}else{
echo "<script>alert('保存失败');window.location.href='数据库操作.php'</script>";
}
}
?>
对数据进行搜索
<style>
table{
border: 1px solid black;
width: 700px;
text-align: center;
margin:0 auto;
}
th{
background-color: aqua;
border: 1px solid black;
}
td{
border: 1px solid black;
}
</style>
<?php
$link = mysqli_connect('127.0.0.1','root','','test','3306');
echo $link?'':'数据库连接失败';
mysqli_query($link,"set names utf8");
if (isset($_POST['ziduan'])) {
$ziduan = $_POST['ziduan'];
}
if (isset($_POST['neirong'])) {
$neirong = $_POST['neirong'];
}
$ziduan = $_POST['ziduan'];
$neirong = $_POST['neirong'];
$query = "SELECT * FROM students WHERE {$ziduan} LIKE '%{$neirong}%'";
$result = mysqli_query($link, $query) or die("执行语句失败");
$count = 0;
echo "<table>";
while ($arr = mysqli_fetch_assoc($result)) {
$count++;
$alt = ($count % 2) ? "alt" : "";
echo "<tr class={$alt}><td>{$arr['id']}</td><td>{$arr['major']}</td>".
"<td>{$arr['class']}</td><td>{$arr['student_id']}</td>".
"<td>{$arr['name']}</td><td>{$arr['grade']}</td><td>{$arr['birthday']}</td>".
"<td><a href=\"数据库修改.php?id={$arr['id']}\">编辑</a> <a href=\"数据库删除.php?id={$arr['id']}\">删除</a></td></tr>";
}
echo "</table>";
mysqli_free_result($result);
?>
代码比较简陋,没有用css进行样式修饰,仅供参考