博主信息
博文 48
粉丝 3
评论 1
访问量 46239
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
sql预处理来实现删除及更新——2018年4月24日
JackBlog
原创
1003人浏览过

GIF.gif

实例


<!DOCTYPE html>
<html lang="zh">
<head>
	<meta charset="UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<meta http-equiv="X-UA-Compatible" content="ie=edge" />
	<title>Document</title>
    <style type="text/css">
    	body{
    		background-color: #BEEBEC;
    	}
        .table_user{
        	width: 80%;
            margin: auto;
            background-color: #8DB3E3;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 2px 2px 2px #3F3F3F;
        }
        .table_user table{
        	text-align: center;
        	width: 100%;
        }
         .table_user  td{
       		width: 14%;
       	}
    </style>
</head>
<body>
	<div class="table_user">
		
		<table border="1" cellspacing="0" cellpadding="5">
			<caption><h3>会员信息表</h3></caption>
			<tr>
				<th>会员id</th>
				<th>账号</th>
				<th>密码</th>
				<th>性别</th>
				<th>金币</th>
				<th>积分</th>
				<th>操作</th>
			</tr>
		<?php
//连接数据库
            require 'inc/mysql.php';
            //创建stmt
            $stmt=mysqli_stmt_init($mysql);
            //sql语句
            $sql = 'select * from t_user where id>? order by id desc';
            //sql语句检测
            if (mysqli_stmt_prepare($stmt,$sql)){
//                查询的参数绑定
                mysqli_stmt_bind_param($stmt,'i',$id);
                $id = 0;
//                执行sql语句
                mysqli_stmt_execute($stmt);
//                取结果集
                mysqli_stmt_store_result($stmt);
//                给结果集里的参数进行绑定
                mysqli_stmt_bind_result($stmt,$id,$username,$password,$sex,$jb,$jf);
//                判断返回数据的行数是否大于0
                if (mysqli_stmt_num_rows($stmt)>0){

//                    取结果集里的数据
						
                    while(mysqli_stmt_fetch($stmt)){
                    	echo '<tr>';
						echo'<td>'.$id.'</td>';
						echo'<td>'.$username.'</td>';
						echo'<td>'.$password.'</td>';
						echo'<td>'.$sex.'</td>';
						echo'<td>'.$jb.'</td>';
						echo'<td>'.$jf.'</td>';
						echo'<td style="width:130px"><a href="acttable_query.php?id='.$id.'&action=update">重置密码</a>  <a href="acttable_query.php?id='.$id.'&action=delete">删除</a></td>';
						echo '</tr>';
                    }
                    
					
					
					
                }else{
                    echo '没有数据';
                }
            }else{
                echo mysqli_stmt_errno($stmt).':'.mysqli_stmt_error($stmt);
            }
//11.注销stmt对象
            mysqli_stmt_free_result($stmt);
            mysqli_stmt_close($stmt);

//12.关闭数据库连接
            mysqli_close($mysql);


?>
		
		
		</table>
	</div>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php

$userid = $_GET['id'];
switch ($_GET['action']) {
    case 'update':
//连接数据库
        require 'inc/mysql.php';
//创建stmt
        $stmt = mysqli_stmt_init($mysql);
//sql语句
        $sql = 'UPDATE  t_user set password = ? where id = ?';
//sql语句检测
        if (mysqli_stmt_prepare($stmt, $sql)) {
//                查询的参数绑定
            mysqli_stmt_bind_param($stmt, 'si', $randpass,$id);
            $randpass = rand(100000,9999999);
            $id = $userid;
//                执行sql语句
            mysqli_stmt_execute($stmt);
//
//                判断返回数据的行数是否大于0
            if (mysqli_stmt_affected_rows($stmt) > 0) {

                echo "<script>alert(\"重置成功,新密码为$randpass\") </script>";
                header("Refresh:0;url=acttable.php");


            } else {
                echo '没有数据被更新';
            }
        } else {
            echo mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt);
        }
//11.注销stmt对象

        mysqli_stmt_close($stmt);

//12.关闭数据库连接
        mysqli_close($mysql);

        break;

    case 'delete':

//连接数据库
        require 'inc/mysql.php';
//创建stmt
        $stmt = mysqli_stmt_init($mysql);
//sql语句
        $sql = 'delete  from t_user where id=?';
//sql语句检测
        if (mysqli_stmt_prepare($stmt, $sql)) {
//                查询的参数绑定
            mysqli_stmt_bind_param($stmt, 'i', $id);
            $id = $userid;
//                执行sql语句
            mysqli_stmt_execute($stmt);

//                判断返回数据的行数是否大于0
            if (mysqli_stmt_affected_rows($stmt) > 0) {
                    $rows = mysqli_stmt_affected_rows($stmt);

                echo "<script>alert(\"成功删除.$rows.条数据\") </script>";
                header("Refresh:0;url=acttable.php");
//
            } else {
                echo "<script>alert(\"没有数据被删除\") </script>";
                header("Refresh:1;url=acttable.php");
            }
        } else {
            echo mysqli_stmt_errno($stmt) . ':' . mysqli_stmt_error($stmt);
        }
//11.注销stmt对象

        mysqli_stmt_close($stmt);

//12.关闭数据库连接
        mysqli_close($mysql);

        break;

}


function jumpto($url){
    header("Location:".$url);
}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:未批改

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学