博客列表 >sql预处理来实现删除及更新——2018年4月24日

sql预处理来实现删除及更新——2018年4月24日

JackBlog
JackBlog原创
2018年04月25日 18:03:01828浏览

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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议