1.mysql
关系型数据库
保存的是结构化的数据。
表头
字段 :
主键:唯一性
行
列
我是xx,今年芳龄18,性别男,来自xxxx,联系方式10086
2.创建表
CREATE TABLE `studentssystem`.`students` ( `sid` INT NOT NULL AUTO_INCREMENT , `sname` VARCHAR(50) NOT NULL , `ssex` VARCHAR(10) NOT NULL , `sage` INT NOT NULL , `sadress` VARCHAR(100) NOT NULL , `sphone` VARCHAR(50) NOT NULL , PRIMARY KEY (`sid`)) ENGINE = MyISAM;
3.增删改查的sql语句
增:INSERT INTO students(sid, sname, `ssex`, `sage`, `sadress`, `sphone`) VALUES (1,"李连杰","男",50,"广西桂林","120")
查:
查所有:select * from students
查students中指定sname,sphone两个字段的所有数据:select sname,sphone FROM students
根据条件查询:select * from students where sid=2 :条件是sid=2
多个条件and查询:select * from students where sname="李连杰" and sphone="110"
多个条件or查询:select * from students where sname="李连杰" or sphone="110"
改:update students set sname="黎明",sage=20,sadress="广东虎门",sphone="114" where sid=2
删:delete from students where sid=1;
4.php与数据库交互:
五个步骤:
a.连接数据源(连接mysql:路径,用户名,密码)
$db = mysqli_connect("localhost","root","root");
b.连接数据库
mysqli_select_db($db,"studentssystem");
c.设置字符编码
mysqli_query($db,"set names utf-8");
d.设置sql语句
$sql = "select * from students where sid=2";
e.执行sql语句
$result = mysqli_query($db,$sql);
如果sql语句是查询,会返回一个结果集,查不到返回空。
如果是其它语句,执行成功返回1,执行失败返回空。
5.浏览器对中文的解码操作
decodeURI(str);(浏览器转码过后的)将中文字符进行解码操作。
encodeURI(str);将中文进行转码操作。
6. 请求和响应(面试官常问的)
请求两种方式get和post区别:
1)数据(传递)携带方式
get:把数据携带在url后作查询串传递。
post:不会显式的放在url后,还是放在请求体中
2)安全性:
get由数据会暴露在url中,所以安全性不高,不适合传递一些敏感性数据
post不会暴露数据,所有安全性较高。
3)数据传递的大小:
传递数据的大小是由浏览器和服务器处理数据的能力决定的。
get:不超过2kb
post:是无限的,
4)性能:
get的传输速度比post快,性能好。post的性能较差。
学生信息的增删改查
1.插入信息1insertStudentsInfo.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="insertStudentInfo.php" method="post">
姓名:<input type="text" name="sname"/><br />
性别:<input type="text" name="ssex"/><br />
年龄:<input type="text" name="sage"/><br />
地址:<input type="text" name="sadress"/><br />
电话:<input type="text" name="sphone"/><br />
<input type="submit" value="插入信息"/>
</form>
</body>
</html>
2.insertStudentInfo.php
<?php
include ("public.php");
$bd=getConnect();
$sname=$_POST["sname"];
$ssex=$_POST["ssex"];
$sage=$_POST["sage"];
$sadress=$_POST["sadress"];
$sphone=$_POST["sphone"];
$sql="insert into students(sname,ssex,sage,sadress,sphone)
values ('$sname','$ssex','$sage','$sadress','$sphone')";
$row=mysqli_query($db, $sql);
if($row){
//插入成功跳转到学生信息页
echo "<script>alert('信息插入成功');location.href='studentInfo.php'</script>";
}else{
echo "<script>alert('信息插入失败');location.href='linserStudentsInfo.html'</script>";
}
?>
3.查询所有信息studentsInfo.php
<?php
include("public.php");
$bd=getConnect();
$sql="select * from students";
$result=mysqli_query($db,$sql);//得到的是一个结果集且输出的是一串数字 print_r ($result);
echo "<table border='1'>
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>住址</th>
<th>联系方式</th>
<th>备注</th>
</tr>";
while($arr1=mysqli_fetch_array($result)){//转成对象形式的数组且每次输出一条
print_r ($arr1);
echo "<tr>
<td>".$arr1['sid']."</td>
<td>".$arr1['sname']."</td>
<td>".$arr1['ssex']."</td>
<td>".$arr1['sage']."</td>
<td>".$arr1['sadress']."</td>
<td>".$arr1['sphone']."</td>
<td><a href='deleteStudent.php?id=".$arr1['sid']."'>删除</a>
<a href='updateStudent.html?id=".$arr1['sid']."&sname=".$sname['sname']."&ssex=".$ssex['ssex']."&sage=".$sage['sage']."&sadress=".$sadress['sadress']."&sphone=".$sphone['sphone']."'>修改</a>
</td>
</tr>";
}
echo"</table>";
echo "<a href='1insertStudentsInfo.html'>添加学信息</a>"
?>
4.删除deleteStudent.php
<?php
include("public.php");
$sid=$_GET["id"];
$db=getConnect();
$sql="delete from students where sid=$sid";
$row=mysqli_query($db,$sql);
if($row){
echo "<script>alert("删除成功");location.href='studentsInfo.php'</script>"
}else{
echo "<script>alert("删除失败");location.href='studentsInfo.php'</script>"
}
?>
5.修改updateStudent.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="updateStudentInfo.php" method="post">
<input class="oinput" type="hidden" name="sid" value="" />
姓名:<input class="oinput" type="text" name="sname"/><br />
性别:<input class="oinput" type="text" name="ssex"/><br />
年龄:<input class="oinput" type="text" name="sage"/><br />
地址:<input class="oinput" type="text" name="sadress"/><br />
电话:<input class="oinput" type="text" name="sphone"/><br />
<input type="submit" value="修改"/>
</form>
</body>
</html>
<script type="text/javascript">
var search=location.search.substr(1);
var oinput=document.getElementsByClassName("oinput");
var arr=decodeURI(search).split("&");
for(var i=0;i<arr.length;i++){
oinput[i].value=arr[i].split("=")[1];
}
</script>
修改updateStudent.html
<?php
include("public.php");
$bd=getConnect();
$sid=$_POST["sid"];
$sname=$_POST["sname"];
$ssex=$_POST["ssex"];
$sage=$_POST["sage"];
$sadress=$_POST["sadress"];
$sphone=$_POST["sphone"];
$sql="update set sname='$sname',ssex='$ssex',sage='$sage',sadress='$sadress',sphone='$sphone'";
$row=mysqli_query($db,$sql);
if($row){
echo "<script>alert('修改成功');location.href='studentsInfo.php'</script>";
}else{
echo "<script>alert('修改失败');location.href='1insertStudentsInfo.html'</script>;
}
?>