【前言】
? ? 本文主要总结下PHP编辑用户信息的功能,原理十分简单,不过语法要求必须正确。笔者在编写过程里遇到不少问题,许多都是符号引起的语法解析错误。这里记录下
?
【踩坑】
(1)连接字符串,因为name等字段类型为char字符串类型,所以在解析的同时要在外面加上' '引号。
? ? ? ? ?所以要写成name=' "$_POST['name']" ',这样才可以解析成name='name'的形式
class="php">$sql = "update demo set name='".$_POST['username']."',age='".$_POST['userage']."'
where id=".$_POST['userid'];
(2)mysqli_fetch_assoc() 函数从结果集中取得一行作为关联数组?,取一条记录
(3)隐藏ID的输入框,常用来传值,<input type='hidden' name='id'>?
?
?
【代码】
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
//连接user数据库
include 'config.php';
$connect = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);
if(!$connect){
die("could not connect:".mysqli_error());
}else{
echo "连接成功<br>";
//查询结果
// mysqli_query() 函数执行一条 MySQL 查询
$result = mysqli_query($connect,"select* from user");
//mysqli_num_rows结果集里行数,该结果集从 mysql_query() 的调用中得到
$count = mysqli_num_rows($result);
//传页的话改为整型
$page = isset($_GET['page'])?(int)$_GET['page']:1;
//每页显示数
$num = 5 ;
//总页数
$total = ceil($count / $num);
if ($page<1) {
$page = 1;
}
if ($page>$total) {
$page = $total;
}
//偏移量
$offset = ($page-1)*$num;
$sql = "select id,name,age from demo order by id asc limit $offset,$num";
$result = mysqli_query($connect,$sql);
if ($result&&mysqli_num_rows($result)) {
echo "<table border=1 cellspacing=0 class='demo'>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>"."<input type='checkbox'>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['age']."</td>";
echo "<td><a href='delete.php?id=".$row['id']."'>删除用户</a></td>";
echo "<td><a href='edit.php?id=".$row['id']."'>编辑用户</a></td>";
echo "<tr/>";
}
echo'<tr>
<td colspan="5">
<a href="index.php?page=1">首页</a>
<a href="index.php?page=' .($page-1). '">上一页</a>
<a href="index.php?page=' .($page+1). '">下一页</a>
<a href="index.php?page=' .$total. '">尾页</a>
当前是第'.$page.'页 共'.$total.'页
</td>
</tr>';
echo "</table>";
echo "<button onclick=''>删除</button>";
}else{
echo "没有数据";
}
}
//关闭连接
$close = mysqli_close($connect);
if(!$close){
die("关闭数据库失败");
}else{
echo "关闭成功";
}
?>
?
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
//连接user数据库
include 'config.php';
$connect = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);
if(!$connect){
die("could not connect:".mysqli_error());
}else{
//编辑用户列表
if (is_numeric($_GET['id'])) {
$id = (int) $_GET['id'];
}
$sql = "select id,name,age from demo where id =".$id;
$result = mysqli_query($connect,$sql);
$data = mysqli_fetch_assoc($result);
}
//关闭连接
$close = mysqli_close($connect);
if(!$close){
die("关闭数据库失败");
}else{
echo "关闭成功";
}
?>
<form action="change.php" method="post">
用户名:<input type="text" name="username" value="<?php echo $data['name'];?>"><br>
年龄:<input type="text" name="userage" value="<?php echo $data['age'] ?>"><br>
<input type="hidden" name="userid" value="<?php echo $data['id']?>">
<input type="submit">
</form>
?
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<?php
//连接user数据库
include 'config.php';
$connect = mysqli_connect(DB_HOST,DB_USER,DB_PWD,DB_NAME);
if(!$connect){
die("could not connect:".mysqli_error());
}else{
// echo $_POST['userid'];
$id = (int) $_POST['userid'];
if (trim($_POST['username'])&&trim($_POST['userage'])) {
$sql = "update demo set name='".$_POST['username']."',age='".$_POST['userage']."' where id=".$_POST['userid'];
$result = mysqli_query($connect,$sql);
if($result){
echo "修改成功<br>";
}else{
echo "修改失败<br>";
}
}
}
//关闭连接
$close = mysqli_close($connect);
if(!$close){
die("关闭数据库失败");
}else{
echo "关闭成功";
}
?>
?
?
?
?
?
?
?
.