Blogger Information
Blog 29
fans 0
comment 0
visits 29286
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
更新用户信息功能
咸鱼梦
Original
839 people have browsed it

输出用户信息表 php:

<?php
//连接数据库
$pageTitle = '用户信息列表';
require 'public/connect.php';
//查询用户信息表user
$sql = "SELECT * FROM user";  //创建查询语句
$result = mysqli_query($db, $sql);  //执行查询
$rows = [];  //创建查询结果容器,初始为空数组,该数组最终会成为一个二维数组,与数组表对应
if ($result && mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
        //print_r($row);  //查看查询到的数据
        $rows[] = $row;  //将每一次查询到的结果放在数组$rows[]中,这是最终返回的结果
} else {
    echo '<h3>数据表为空,没有数据~~</h3>';
}

//释放结果集
mysqli_free_result($result);
//关闭连接
mysqli_close($db);

?>
<?php include 'public/header.php'?>
<!--创建表格用来展示用户数据-->
<div class="container">
	<div class="row">
		<div class="com-md-12">
			<h3>用户信息表</h3>
			<table class="table table-bordered text-center">
				<tr class="h5 bg-info">
					<td>ID</td>
					<td>姓名</td>
					<td>邮箱</td>
					<td>操作</td>
				</tr>
				<?php foreach ($rows as $row) : ?>
					<tr>
						<td>
							<?php echo $row['id']?>
						</td>
						<td>
							<?php echo $row['name']?>
						</td>
						<td>
							<?php echo $row['email']?>
						</td>
						<td>
							<a class="btn btn-primary" href="edit.php?id=<?php echo $row['id']?>" role="button">编辑</a>
							<a class="btn btn-danger" href="del.php?id=<?php echo $row['id']?>" role="button">删除</a>
						</td>
					</tr>
				<?php endforeach;?>
			</table>
		</div>
	</div>
</div>

<!--公用底部-->
<?php include 'public/footer.php'?>

更新数据功能实现:

<?php 
$pageTitle = '更新数据';
include 'public/header.php';
//获取要编辑的id
$id = $_GET['id']; 
//连接数据库
require 'public/connect.php';
//创建sql查询数据
$sql = "SELECT * FROM `user` WHERE `id` = ".$id;
//执行查询
$res = mysqli_query($db,$sql);
if ($res && mysqli_num_rows($res) > 0) {
	 //根据主键查询只会返回一条记录,不用循环处理
	$row = mysqli_fetch_array($res, MYSQLI_ASSOC);
}
//print_r($row);

?>
<div class="container">
        <div class="row">
            <div class="col-md-12">
                <h3 class="text-center">用户信息编辑</h3>
<!--                修改处理的php脚本名称-->
                <form class="form-horizontal" action="do_edit.php?id=<?php echo $row['id'] ?>" method="post">
                    <div class="form-group">
<!--                        用户名和密码是不允许修改的,因为这是登陆的重要信息,仅允许修改邮箱-->
                        <label for="name" class="col-sm-2 control-label">用户名</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="name" placeholder="userName" name="name" value="<?php echo $row['name'] ?>" readonly>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="email" class="col-sm-2 control-label">邮箱</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="email" placeholder="Email" name="email" value="<?php echo $row['email'] ?>">
                        </div>
                    </div>


                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10  text-center">
                            <button type="submit" class="btn btn-primary  btn-block">保存</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    
<?php include 'public/footer.php'?>
<?php
$pageTitle = '更新处理';
include 'public/header.php';
//对用户的注册请求进行处理
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$error = []; //创建一个空数组,用来接收出错信息
	//检测邮箱
	if (empty($_POST['email'])) {
		$error[] = '没有输入邮箱';
	} else {
		$email = trim($_POST['email']);
	}
	
	//如果错误信息数组空,表时注册操作正确,将用户添加到数据库中
    if (empty($error)) {
        //连接数据库
        require 'public/connect.php';
        //创建sql语句
//        $sql = "INSERT user1 SET `name`='{$name}', `email`='{$email}', `password`=sha1('{$password}')";
        $sql = "UPDATE user SET  `email`='{$email}' WHERE `id`={$_GET['id']}";  //直接修改一个关键字由INSERT换成UPDATE,把后面的name和password删除
//        $sql = "INSERT user1 (`name`,`email`,`password`) VALUES ('{$name}','$email',sha1('$password'))";

        //执行插入操作
        if (mysqli_query($db, $sql)){
            echo '<h3 style="color: green">成功的更新了'.mysqli_affected_rows($db).'条记录</h3>';
        } else {
            echo '<h3 style="color:red">更新失败:'.mysqli_error($db).'</h3>';
        }
        //关闭连接
        mysqli_close($db);
    }  else {
        //打印出错误信息
        foreach ($error as $message) {
            echo '<h3 style="color:red">'.$message.'</h3>';
        }
    }
	
}
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post