本人最近做网站,遇到用PHP对数据库的增删改操作,接着上两篇,记录修改数据是怎么操作的。
1、这个可以作为主文件:(这一段是HTML+css的基础)
<div id="id03" class="modal">
<span onclick="document.getElementById('id03').style.display='none'" class="close2"
title="Close Modal">×</span>
<form class="modal-content animate" method="get" action="edit.php">
<div class="container">
<h1>用户信息修改</h1>
<br />
<label><b>所有用户信息</b></label>
<!-- <textarea cols="30" rows="20">默认文本</textarea> -->
<table cellpadding=0 cellspacing=0 >
<thead>
<tr style="color:red;">
<th>用户名</th>
<th>用户密码</th>
<th>用户级别</th>
<th>操作</th>
</tr>
</thead>
<tbody >
<tr >
<td>admin</td>
<td>admin</td>
<td>admin</td>
<td>禁止修改</td>
</tr>
<?php
$sql="select * from userinfo";
$res=mysqli_query($con,$sql);
while($row=$res->fetch_assoc()){
//var_dump($row);
?>
<tr >
<td><?php echo $row["username"];?> </td>
<td><?php echo $row["password"];?> </td>
<td><?php echo $row["level"];?> </td>
<td>
<a href="edit.php?id=<?php echo $row['id']?>">修改信息</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
这个代码界面如图:
2、本文章重点看这段php代码:
edit.php代码
<?php
error_reporting(0);//加上error_reporting(0);就不会弹出警告了
include('conn.php');
$id = $_GET["id"];
//根据id查到当前要操作的数据
$sql= "select * from userinfo where id = '$id'";
//查询
$result=$con->query($sql);
// var_dump($result);//dedao
//得到具体信息
$res = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>修改系统</title>
<link href="css/init.css" rel="stylesheet" />
<link href="css/init.css" rel="stylesheet" />
<link href="css/xitong.css" rel="stylesheet" />
</head>
<body>
<form class="modal-content animate" action="go.php" method="get" >
<div class="container">
<h1>修改用户信息</h1>
<br />
<hr />
<input type="hidden" value="<?php echo $res['id'];?> " name="id" >
<label><b>修改的用户姓名</b></label>
<input type="text" value="<?php echo $res['username'];?> " name="username">
<label><b>用户密码</b></label>
<input type="password" value="<?php echo $res['password'];?> " name="password" >
<label><b>用户级别:(修改为admin或者common,其中common代表普通用户,admin代表管理员用户)</b></label>
<input type="text" value="<?php echo $res['level'];?> " name="level">
<!-- <input type="radio" name="level" value="<?php echo $res['level'];?>" id="pu" />
<label for="pu">普通用户   </label>
<input type="radio" name="level" value="<?php echo $res['level'];?>" id="guan" />
<label for="guan">管理员</label> -->
<div class="clearfix">
<button type="submit" name="insub" class="signupbtn">修改信息</button>
<button type="reset" onclick="document.getElementById('id01').style.display='none'"
class="cancelbtn">返回</button>
</div>
</div>
</form>
go.php代码:
<?php
error_reporting(0);//加上error_reporting(0);就不会弹出警告了
include('conn.php');
$id = $_GET["id"];
$user = $_GET['username'];
$pwd = $_GET['password'];
$level = $_GET['level'];
//根据id查到当前要操作的数据
$sql= "update userinfo set username = '$user', password ='$pwd',level='$level' WHERE id = '$id'";
//执行
$result=$con->query($sql);
if(!$result )
{
echo(" 修改失败\n") ;
// die('删除失败: ' . mysqli_error($con));
}
else{
echo " 修改成功\n";
}
//header("Location:xitong-admin.php");//回到首页
?>
3、最终实现了功能