php连接数据库的增删改查

php的连接数据库查询

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询</title>
<style>
h1,form{
text-align: center;
}
table{
margin: auto;
border: 2px solid blue;
}
table{
margin:auto;
border:2px solid blue;
}
tr:nth-child(odd){
background: #ccc;
}
tr:nth-child(even){
background: yellow;
}
.form01{
margin:auto;
height: 74px;
}
</style>
<script>
function check1(){
var db = document.getElementById("bd");
db.action = "./insert.php";
db.submit();
}
function check2(){
var db = document.getElementById("bd");
db.action = "./update.php";
db.submit();
}
function check3(){
var db = document.getElementById("bd");
db.action = "./delete.php";
db.submit();
}
</script>
</head>
<body>
<h1>所有老师信息</h1>
<div class="form01">
<form action='./index.php' method='post'>
<input type='text' name='query'/>
<input type='submit' name='btnQuery' value='搜索'/>
</form>
<br/>
<form>
<form action='' method='post' id='bd'>
<input type='submit' name='insert' value='新增' onclick='check1()'>
<input type='submit' name='' value='编辑' onclick='check2()'>
<input type='submit' name='delete' value='删除' onclick='check3()'>
</div>
<table border="1" width='600px' style='text-align:center;'>
<tr>
<td></td>
<td>工号</td>
<td>姓名</td>
<td>性别</td>
<td>出生日期</td>
<td>所在部门</td>
</tr>
<?php
$query = $_POST['query'];
//$_SESSION['query']="$query";
$db = new mysqli("localhost", "root","123456","studentmis");
if(!$db)
{
echo "数据库连接失败!!!";
}
if(isset($_POST['btnQuery']) && $query != null)
{
//session_start();
$sql="SELECT laoshi.e_id,laoshi.e_name, laoshi.sex, laoshi.date_birth, bumen.dept_name
from laoshi left join bumen on bumen.dept_no = laoshi.dept_no WHERE laoshi.e_id = '$query' or laoshi.e_name = '$query' or bumen.dept_name = '$query'";
}else{
$sql="SELECT e_id,e_name, sex, date_birth, bumen.dept_name from laoshi left join bumen on bumen.dept_no = laoshi.dept_no";
}
$db->query("SET NAMES utf8");
$res=$db->query($sql);
?>
<?php
if($res){

while($row=$res->fetch_assoc()) {
$gonghao=$row['e_id'];
$xingming=$row['e_name'];
$sex=$row['sex'];
$shengri=$row['date_birth'];
$suozaibumen=$row['dept_name'];

echo "<center><tr><td><input type='checkbox' name='sel[]' value='".$row["e_id"]."'/></td></center>".
"<td>$gonghao</td>
<td>$xingming</td>
<td>$sex</td>
<td>$shengri</td>
<td>$suozaibumen</td>
</tr>";
}

//释放结果集
$res->close;
}
//关闭连接
$db->close();
?>
</table>
</form>
</body>
</html>

php连接数据库插入

insert.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>新增</title>
<style type="text/css">
.css-01
{
width: 100px;
height: 50px;
margin: 200px auto;
}
</style>
<script type="text/javascript">
function myCheck()
{//遍历所有表单元素
for(var i=0;i<document.form1.elements.length-1;i++)
{//判断所有表单值是否为空
if(document.form1.elements[i].value=="")
{
alert("当前表单不能有空项");
//将光标定位在空表单处
document.form1.elements[i].focus();
return false;
}
}
return true;
}
</script>

</head>
<body>
<div class="css-01">
<form action="./insert.php" name="form1" method="post" onSubmit="return myCheck()">
<input type="text" name="id" placeholder="ID">
<input type="text" name="name" placeholder="姓名">
<input type="text" name="birthday" placeholder="生日">
<?php
echo "<select name='department'>";
$db = new mysqli("localhost","root","123456","studentmis");
$sql = "SELECT * FROM bumen WHERE dept='xinguan'";
$db->query("SET NAMES utf8");
$data = $db->query($sql);
if($data){
while($row = $data->fetch_assoc()) {
$bumen_id = $row['dept_no'];
$bumen_name = $row['dept_name'];
echo "<option name='bumen' value='$bumen_id'>$bumen_name</option>";
//echo "<input type='text' name='department' placeholder='部门'>";
}
}
echo "</select>";
?>
<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
<input type="submit" name="submit" value="新增">
</form>
</div>
<?php
if(isset($_POST['submit'])){
$id = $_POST["id"];
$name = $_POST["name"];
$sex = $_POST["sex"];
$birthday = $_POST["birthday"];
$department = $_POST["department"];
$db = mysqli_connect("localhost", "root","123456","studentmis");
if(!$db){
echo "数据库连接失败!!!";
}
$mysql = "INSERT INTO laoshi VALUES ('$id','$name','$sex','$birthday','$department')";
mysqli_query($db,"SET NAMES utf8");
$data = mysqli_query($db,$mysql);
if ($data) {
echo "<script>alert('添加成绩成功');this.location.href='index.php'</script>";
} else {
echo "<script>alert('添加成绩失败')</script>";
}
mysqli_close($db);
}
?>
</body>
</html>

php连接数据库删除

delete.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>删除</title>
</head>
<body>
<?php
if(count($_POST['sel']) == 0){
echo "<script>alert('请先选择需要删除或修改的老师信息!');history.go(-1);</script>";
}else{
$db = new mysqli ("localhost", "root","123456","studentmis");
if(!$db){
echo "数据库连接失败!!!";
}
for($i=0;$i<count($_POST['sel']);$i++){
$sqlDel ="DELETE FROM laoshi WHERE e_id='".$_POST['sel'][$i]."'";
$db->query($sqlDel);
}
echo "<script>alert('删除成功!');window.location='index.php';</script>";
$db->close();
}
?>
</body>
</html>

php连接数据库编辑

update.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Conter-Type" content="text/html; charset=utf8"/>
<title>更新</title>
</head>
<body>
<?php
if(count($_POST['sel'])==0 && !isset($_POST['update'])) //判断是否选择了要删除的选项,并且没有点击本页面的提交按钮
{
echo "<script>alert('请先选择需要删除或修改的老师信息!');history.go(-1);</script>";
}
$db = new mysqli("localhost","root","123456","studentmis");
if($db->connect_error)
{
echo "数据库连接失败!!!";
}
if(isset($_POST['update'])){ //点击按钮执行更新
$gonghao = $_POST["text1"];
$xingming = $_POST["text2"];
$shengri = $_POST["text3"];
$bumen = $_POST["department"];
$sex = $_POST["sex"];

$sql = "UPDATE laoshi SET e_id='$gonghao',e_name='$xingming',sex='$sex',date_birth='$shengri',dept_no='$bumen' WHERE e_id='$gonghao'";
$db->query('SET NAMES UTF8');
$result = $db->query($sql);
if ($result) {
echo "<script>alert('更新成功');this.location.href='index.php'</script>";
} else {
echo "<script>alert('更新失败')</script>";
}
}else{ // 查询要更新的数据
for($i=0;$i<count($_POST['sel']);$i++){
$sqlDel ="SELECT e_id,e_name, sex, date_birth,dept_no
FROM laoshi WHERE e_id='".$_POST['sel'][$i]."'";
$db->query('SET NAMES UTF8');
$res=$db->query($sqlDel);
if($res){
while($row=$res->fetch_assoc())
{
$gonghao=$row['e_id'];
$xingming=$row['e_name'];
$sex=$row['sex'];
$shengri=$row['date_birth'];
$suozaibumen=$row['dept_no'];
}
echo "<form action='./update.php' method='post'>";
echo "<div style='margin: 200px auto;width: 100px;height: 50px;'>
<input type='text' placeholder='工号' name='text1' value='$gonghao' readonly='readonly'>
<br>
<input type='text' placeholder='姓名' name='text2' value='$xingming'>
<br>
<input type='text' placeholder='生日' name='text3' value='$shengri'>
<br>";
// 下拉框
echo "<select name='department'>";
$sql = "SELECT * FROM bumen WHERE dept='xinguan'";
$db->query("SET NAMES utf8");
$data = $db->query($sql);
if($data){
while($row = $data->fetch_assoc()) {
$bumen_id = $row['dept_no'];
$bumen_name = $row['dept_name'];
if($bumen_id == $suozaibumen){
echo "<option name='bumen' value='$bumen_id' selected>$bumen_name</option>";
}else{
echo "<option name='bumen' value='$bumen_id'>$bumen_name</option>";
}

}
}
echo "</select>";

if($sex == "男")
{
echo "<input type=radio name=sex value='男' checked>男";
echo "<input type=radio name=sex value='女'>女";
}
else
{
echo "<input type=radio name=sex value='男'>男";
echo "<input type=radio name=sex value='女' checked>女";
}
echo "<br>";
echo "<input type='submit' name='update' value='提交'>";
echo "</div>";
echo "</form>";
//释放结果集
$res->close;
}
//关闭连接
}
}
$db->close();
?>
</body>
</html>