本是根据网络上一些代码,不过代码已旧,此处修改为新版代码,同样是增删改查,提供给新入门的朋友。
QQ:1106408182
原文链接:寻找源头>>
连接数据库:
<?php
function connnetDb(){
//连接mysql数据库
$conn=mysqli_connect("127.0.0.1:3366","root","vertrigo");
//排除连接数据库异常错误
if(!$conn){
die('can not connect db');
}
//在mysql中选中myapp数据库
$conn->select_db("user");
return $conn;
}
主界面:
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>所有用户</title>
<style>
table{
border-collapse: collapse;
}
th,td{
border:1px solid #ccccff;
padding: 5px;
}
td{
text-align: center;
}
</style>
</head>
<body>
<a href="cadd.php">添加用户</a>
<table>
<tr><th>id</th><th>名字</th><th>年龄</th><th>修改/删除</th></tr>
<?php
//连接数据库
$conn = connnetDb();
//查询数据表中的所有数据,并按照id降序排列
$result=$conn->query("SELECT * FROM user ORDER BY id DESC");
//获取数据表的数据条数
$dataCount=$result->num_rows ;
//echo $dataCount;
//打印输出所有数据
for($i=0;$i<$dataCount;$i++){
$result_arr=$result->fetch_assoc();
$id=$result_arr['id'];
$name=$result_arr['name'];
$age=$result_arr['age'];
//print_r($result_arr);
echo "<tr><td>$id</td><td>$name</td><td>$age</td><td><a href='cedit.php?id=$id'>修改</a>
<a href='dele.php?id=$id'>删除</a></td></tr>";
}
?>
</table>
</body>
</html>
增加的界面:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>添加用户</title>
</head>
<body>
<form action="add.php" method="post">
<label>用户名:</label><input type="text" name="name">
<label>年龄:</label><input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
增加:
<?php
require_once 'func.php';
//首先进行非空排错
if(!isset($_POST['name'])){
die('name is not define');
}
if(!isset($_POST['age'])){
die('age is not define');
}
$name=$_POST['name'];
$age=$_POST['age'];
if(empty($name)){
die('name is empty');
}
if(empty($age)){
die('age is empty');
}
//连接数据库
$conn = connnetDb();
//执行类型转换,防止SQL注入
$age=intval($age);
//插入数据
$conn->query("INSERT INTO user(name,age) VALUES ('$name',$age)");
//返回列表页面
if($conn->error){
echo $conn->error;
}else{
header("Location:sele.php");
}
删除:
<?php
require_once 'func.php';?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改用户数据</title>
</head>
<body>
<?php
if(!empty($_GET['id'])){
//连接mysql数据库
$conn = connnetDb();
//查找id
$id=intval($_GET['id']);
$result=$conn->query("SELECT * FROM user WHERE id=$id");
if($conn->error){
die('can not connect db');
}
//获取结果数组
$result_arr=$result->fetch_assoc();
}else{
die('id not define');
}
?>
<form action="edit.php" method="post">
<label>用户ID:</label><input type="text" name="id" value="<?php echo $result_arr['id']?>">
<label>用户名:</label><input type="text" name="name" value="<?php echo $result_arr['name']?>">
<label>用户年龄:</label><input type="text" name="age" value="<?php echo $result_arr['age']?>">
<input type="submit" value="提交修改">
</form>
</body>
</html>
修改的界面:
<?php
require_once 'func.php';?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改用户数据</title>
</head>
<body>
<?php
if(!empty($_GET['id'])){
//连接mysql数据库
$conn = connnetDb();
//查找id
$id=intval($_GET['id']);
$result=$conn->query("SELECT * FROM user WHERE id=$id");
if($conn->error){
die('can not connect db');
}
//获取结果数组
$result_arr=$result->fetch_assoc();
}else{
die('id not define');
}
?>
<form action="edit.php" method="post">
<label>用户ID:</label><input type="text" name="id" value="<?php echo $result_arr['id']?>">
<label>用户名:</label><input type="text" name="name" value="<?php echo $result_arr['name']?>">
<label>用户年龄:</label><input type="text" name="age" value="<?php echo $result_arr['age']?>">
<input type="submit" value="提交修改">
</form>
</body>
</html>
修改:
<?php
require_once 'func.php';
if(empty($_POST['id'])){
die('id is empty');
}
if(empty($_POST['name'])){
die('name is empty');
}
if(empty($_POST['age'])){
die('age is empty');
}
$id=intval($_POST['id']);
$name=$_POST['name'];
$age=intval($_POST['age']);
//连接数据库
$conn = connnetDb();
//修改指定数据
$conn->query("UPDATE user SET name='$name',age=$age WHERE id=$id");
//排错并返回
if($conn->error){
echo $conn->error;
}else{
header("Location:sele.php");
}