一.增
$REQUSET['']; 接收数据
eg:$sex=$.REQUSET['sex'];
当出现undefined时,指没有数据,利用查询字符串的方式(路由传参)
2.>创建一个.html文件--------传到,php文件中;
在html中写表单;
表单中的name属性是提交时,将前端中表单的内容传到后台;
注意:1.》最外层用双引时,括号内用单引;
2.>刷新一次,执行一次,因为primary key相同的编号只能用一次;
3.>当出现乱码时
mysql_uroot;
use jd;
select * from jd;
4.>举例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form action="4.php">
<p>
名字:<input type="text" name="sname"/>
</p>
<p>
性别:<input type="" name="sex"/>
</p>
<p>
年龄:<input type="text" name="age"/>
</p>
<p>
<input type="submit" name="" id="" value="提交" />
</p>
</form>
</body>
</html>
<?php
//$sid="金庸先生的离世";
$sname=$_REQUEST['sname'];
$sex=$_REQUEST['sex'];
$age=$_REQUEST['age'];
#1.连接数据库
$conn=mysqli_connect('127.0.0.1','root','','test',3306);
#2.设置编码
$sql='SET NAMES UTF8';
mysqli_query($conn,$sql);
#3.执行sql语句
$mysql="INSERT INTO suu values(null,'$sname','$sex','$age')";
$result=mysqli_query($conn,$mysql);
#4.结果
if($result===true){
echo "插入成功";
}
else{
echo "插入失败";
}
二.删除
1.$mysql="delete from ph where sname='$sname'";
切记:在htdocs里命名时,不能用中文;不能有空格,空格占一个字符;
三
1..查找
执行select语句时,不输出true和false值,而是输出所有的结果集;
$sql="select * from phone";
$all=mysqli_fetch_all($result);//把查询到的所有结果,按照数组的格式输出;
2.数组的格式输出
数组分为索引数组和关联数组;
索引数组:下标是数字,有length属性,数组的长度=最大下标+1;
关联数组:下标不是数字,没有length属性;
select 把查找到东西显示到某一区域;本例是把数据输出到table中;
3.把数据按照索引数组输出;
1.$all=mysqli_fetch_all($result); 把数据按照索引数组输出;
2.$row=mysqli_fetch_asssoc($result); 把数据按照关联数组输出;
四.举例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
li{
list-style: none;
}
a{
text-decoration: none;
}
.list{
width: 800px;
margin: 10px auto;
border-top: 2px solid #000;
}
a,span,li{
font-size: 15px;
}
.list-top .a{
margin-left: 400px;
}
.list-top .time{
margin-left: 30px;
}
.list-top li:nth-child(2){
margin: 10px 0px 10px 0px;
}
</style>
</head>
<body>
<?php
require('init.php');
$sql="select * from msg";
$result=mysqli_query($conn,$sql);
if($result===true){
echo 'err';
}else{
echo "<div class='box'>";
while(true){
$row=mysqli_fetch_assoc($result);
if($row===null){
break;
}else{
echo"<div class='list'>";
echo "<ul class='list-top'>";
echo "<p><span>发布人:$row[uname]</span> <span class='time'> 发布时间:$row[pubTime]</span><span class='a'><a href='msg_delete.php?mid=$row[mid]' onclick='return del()'>X</a></span></p>";
echo "<li>联系电话:$row[phone]</li>";
echo "<li>发布内容:$row[content]</li>";
echo "</ul>";
echo "<div>";
}
}
echo "</div>";
}
?>
<script>
function del(){
if(confirm("确定要删除吗")){
alert('删除成功!');
return true;
}else{
alert('删除失败!');
return false;
}
}
</script>
</body>
</html>
网友评论