使用sqli方法连接mysql数据库的必备步骤:
头部声明信息(解决编码格式不统一问题)
顺便记录最简单的数据库增删改查
由于数据库是接触不深,就简单的记录下数据库的最简单的操作
当然代码没有进行优化。重复部分很多。慢慢来。以后深入了解在优化
list列表页面
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<a href="add1.php" class="btn btn-success">新增信息</a>
<?php
header("Content-Type: text/html;charset=utf-8");
//1.连接数据库
$link=mysqli_connect("localhost","root","root");
//2.判断是否链接成功
if(!$link){
exit("数据库链接失败");
}
//3.设置字符集
mysqli_set_charset($link,"utf8");
//4.选择数据库
mysqli_select_db($link,"student");
//5.准备sql语句
$sql="select * from stu_msg";
//6.发送sql语句
$res=mysqli_query($link,$sql);
echo "<table width=500 border=1 class='table'>";
echo "<thead class='text-center'>";
echo "<th>编号</th><th>姓名</th><th>性别</th><th>年龄</th><th>操作</th>";
echo "</thead><tbody>";
while($rows=mysqli_fetch_assoc($res)){
echo "<tr>";
echo "<td>".$rows["id"]."</td>";
echo "<td>".$rows["name"]."</td>";
echo "<td>".($rows["sex"]==1?"男":"女")."</td>";
echo "<td>".$rows["age"]."</td>";
echo '<td align=center><a href="delet.php?id='.$rows['id'].'">删除</a><a href="updata.php?id='.$rows['id'].'">修改</a></td>';
echo "</tr>";
}
echo "</tbody></table>";
//8.关闭数据库
mysqli_close($link);
?>
</div>
</div>
</body>
</html>
add页面
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="panel">
<div class="panel-heading panel-info">新增信息</div>
<div class="panel-body">
<?php
header("Content-Type: text/html;charset=utf-8");
$uname=$_POST["uname"];
$uage=$_POST["uage"];
$usex=$_POST["usex"];
//1.连接数据库
$link=mysqli_connect("localhost","root","root");
//2.判断是否链接成功
if(!$link){
exit("数据库链接失败");
}
//3.设置字符集
mysqli_set_charset($link,"utf8");
//4.选择数据库
mysqli_select_db($link,"student");
//5.准备sql语句
$sql="insert into stu_msg (name,sex,age) value ('$uname','$usex','$uage')";
//6.发送sql语句
$res=mysqli_query($link,$sql);
if($res && mysqli_affected_rows($link)){
echo "添加成功 !<a href='php_list.php'>返回列表页</a>";
}else{
echo "添加失败";
}
mysqli_close($link);
?>
</div>
</div>
</div>
</div>
</body>
</html>
add1页面
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="panel col-lg-5">
<div class="panel-heading">新增信息</div>
<div class="panel-body">
<?php
header("Content-Type: text/html;charset=utf-8");
?>
<form action="add.php" method="post" >
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">姓名</span>
<input type="text" name="uname" class="form-control" aria-describedby="basic-addon1">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">性别</span>
<input type="text" name="usex" class="form-control" aria-describedby="basic-addon1">
</div>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">年龄</span>
<input type="text" name="uage" class="form-control" aria-describedby="basic-addon1">
</div>
<input type="submit" value="提交" class="btn btn-info text-center"/>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
delete页面
<?php
header("Content-Type: text/html;charset=utf-8");
//获取出来要删除的那一条记录
$id=$_GET["id"];
//1.连接数据库
$link=mysqli_connect("localhost","root","root");
//2.判断是否链接成功
if(!$link){
echo "数据库链接失败";
}
//3.设置字符集
mysqli_set_charset($link,"utf8");
//4.选择数据库
mysqli_select_db($link,"student");
//5.准备sql语句
$sql="delete from stu_msg where id=$id";
//6.发送sql语句
$res=mysqli_query($link,$sql);
if($res && mysqli_affected_rows($link)){
echo "删除成功 !<a href='php_list.php'>返回列表页</a>";
}else{
echo "删除失败";
}
mysqli_close($link);
?>
updata页面
<?php
header("Content-Type: text/html;charset=utf-8");
$id=$_GET["id"];
//1链接数据库
$link=mysqli_connect("localhost","root","root");
//2.判断链接是否成功
if(!$link){
exit("数据库链接失败!");
}
//3.设置字符集
mysqli_set_charset($link,"utf8");
//4.选择数据库
mysqli_select_db($link,"student");
//5.准备sql语句
$sql="select * from stu_msg where id=$id";
//6.发送sql语句
$res=mysqli_query($link,$sql);
$rows=mysqli_fetch_assoc($res);
echo "<pre>";
echo $rows;
mysqli_close($link);
?>
<form action="doupdata.php" method="post">
<input type="hidden" name="pid" value="<?php echo $id?>" />
姓名:<input type="text" name="uname" id="" value="<?php echo $rows["name"]; ?>" />
年龄:<input type="text" name="uage" id="" value="<?php echo $rows["age"]; ?>" />
性别:<input type="text" name="usex" id="" value="<?php echo $rows["sex"]; ?>" />
<input type="submit" value="确认修改"/>
</form>
doupdata页面
<?php
header("Content-Type: text/html;charset=utf-8");
$pid=$_POST["pid"];
$uname=$_POST["uname"];
$uage=$_POST["uage"];
$usex=$_POST["usex"];
//1链接数据库
$link=mysqli_connect("localhost","root","root");
//2.判断链接是否成功
if(!$link){
exit("数据库链接失败!");
}
//3.设置字符集
mysqli_set_charset($link,"utf8");
//4.选择数据库
mysqli_select_db($link,"student");
//5.准备sql语句
$sql="update stu_msg set name='$uname',sex='$usex',age='$uage' where id=$pid";
//echo $sql;
//6.发送sql语句
$res=mysqli_query($link,$sql);
if($res && mysqli_affected_rows($link)){
echo "更新成功!<a href='php_list.php'>返回列表页</a>";
}else{
echo "更新失败";
}
mysqli_close($link);
?>