老师,页面转账和搜索不太明白,给点思路吧
index.php <?php include('E:/myphp_www/PHPTutorial/WWW/php_test/imooc_mysql_db/mysqldemo_static/lib/photos.php'); header('content-type:text/html;charset=utf8'); session_start();//开启session //连接数据库 $conn = new mysqli('localhost','root','root','imooc_test'); $conn->query('set names utf8');//设定编码方式 if (!$conn){//检查数据库连接是否正确 echo '数据库连接失败'.mysqli_connect_error(); } $sql = "select id,name,number,money,info,face from student"; $result = $conn->query($sql);//执行语句 if ($result){ while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)){ $rows[] = $row; } }else{ die('无法读取数据'.mysqli_error($conn)); } ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>用户转账</title> <link href="style/css/bootstrap.min.css" rel="stylesheet"> <link href="style/css/site.min.css" rel="stylesheet"> <style> .container .row .col-lg-4 img{ display: block; margin-left: auto; margin-right: auto; } .container .row .col-lg-4 h3,p{ text-align: center; } .row .col-lg-4 .button{ width: 360px; margin-left: 150px; margin-bottom: 10px;} </style> </head> <body> <!--导航栏--> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand hidden-sm" href="index.html">慕课网 <?php echo $_SESSION['name']?>,欢迎您</a> </div> <div class="navbar-header" style="display:none;"> <a class="navbar-brand hidden-sm" href=""><?php echo $_SESSION['name']?>,欢迎您!</a> </div> </div> </div> <!--导航栏结束--> <!--巨幕--> <div class="jumbotron masthead"> <div class="container"> <h1>学生转账管理系统</h1> <h2>实现学生转账功能</h2> <p class="masthead-button-links"> <form class="form-inline" action="login_class.php" method="post"> <div class="form-group"> <input type="text" class="form-control" id="exampleInputName2" placeholder="输入搜索内容" name="key" value=""> <button class="btn btn-default" name="key" type="submit">搜索</button> <a href="register.html"><button type="button" class="btn btn-primary btn-default" data-toggle="modal">注册</button></a> <a href="login.html"><button type="button" class="btn btn-primary btn-default" data-toggle="modal">登录</button></a> <a href="login_class.php"><button type="button" class="btn btn-primary btn-default" data-toggle="modal" name="sign_out">退出</button></a> </div> </form> </p> </div> </div> <!--巨幕结束--> <!-- 模态框 --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <form class="form-inline" action="transfer_class.php" method="post" enctype="multipart/form-data"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">转账</h4> </div> <div class="modal-body"> <p> 收款人: <select class="form-control"> <option value="">user1</option> <option value="">user2</option> <option value="">user3</option> </select> </p> <br /> <p>转账金额:<input type="text" class="form-control" id="exampleInputEmail1" name="moneyinfo" placeholder="请输入数字"></p> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary" name="submit" id="submit" onclick="show(this)">确认转账</button> <button type="reset" class="btn btn-default">重置</button> </div> </div> </form> </div> </div> <!--模态框结束--> <div class="container projects"> <div class="projects-header page-header"> <h2>用户展示</h2> <p>将用户信息展示在页面中</p> </div> <!--信息展示--> <div class="row"><?php foreach ($rows as $val){?> <div class="col-lg-4"> <img class="img-circle" src="upload/12.jpg" alt="" width="140" height="140"> <h3><?php echo $val['name'];?></h3> <p><?php echo $val['info'];?></p> <div class="button"> <button type="button" class="btn btn-primary btn-default" data-toggle="modal" data-target="#myModal"> 转账 </button> </div> </div><?php }?> <div class="col-lg-4"> </div> </div> </div> <footer class="footer container"> <div class="row footer-bottom"> <ul class="list-inline text-center"> <h4><a href="http://class.imooc.com" target="_blank">class.imooc.com</a> | 慕课网</h4> </ul> </div> </footer> <script src="style/js/jquery.min.js"></script> <script src="style/js/bootstrap.min.js"></script> </body> </html> login_class.php <?php session_start(); //开启session //接受登陆数据 $name = $_POST['name']; $number = $_POST['number']; //建立数据库连接并设定字符集 $conn = new mysqli('localhost','root','root','imooc_test'); mysqli_set_charset($conn,"utf8"); if (!$conn){//检查数据库连接 echo '数据库连接失败'.mysqli_connect_error($conn); } $sql = "select name,number from student where name='$name',number='$number'"; $result = $conn->query($sql); if (!$result){ echo '登录失败';//.mysqli_error($conn);header('refresh:2; url=login.html');//检验注册是否成功 }else{ header('refresh:2; url=index.php'); } // 判断提交的登录信息 if ($name == ''){ //若为空,视为未填写,提示错误,并1秒后返回登录界面 header('refresh:2; url=login.html'); echo "姓名不能为空,系统将在2秒后跳转到登录界面,请重新填写登录信息!"; exit; } if ($name !== 'name' && $number !== 'number') { echo '用户名或学号不正确'; }else { exit('登录成功'); } //用户名和密码都正确,将用户信息存到Session中 $_SESSION['name'] = $name; $_SESSION['islogin'] = 1; // 处理完附加项后跳转到登录成功的首页 header('location:index.php'); if(!$_POST['sign_out']){ setcookie('name', $name, time()+7*24*60*60); setcookie('code', md5($name.md5($number)), time()+7*24*60*60); }else{ // 删除Cookie setcookie('name', '', time()-999); setcookie('code', '', time()-999); } // 处理完附加项后跳转到登录成功的首页 header('location:index.php'); if($row){//匹配用户名和学号 if($number != $row['number'] || $name !=$row['name']){ echo "<script>alert('用户名或学号错误,请重新输入');location='login.html'</script>"; exit; } else{ $_SESSION['name']=$row['name']; $_SESSION['number']=$row['number']; echo "<script>alert('登录成功');location='index.php'</script>"; } } else{ echo "<script>alert('您输入的用户名不存在');location='login.html'</script>"; exit; } register.php <?php include('E:/myphp_www/PHPTutorial/WWW/php_test/imooc_mysql_db/mysqldemo_static/lib/photos.php'); header('content-type:text/html;charset=utf8'); session_start();//开启session //连接数据库 $conn = new mysqli('localhost','root','root','imooc_test'); $conn->query('set names utf8');//设定编码方式 if (!$conn){//检查数据库连接是否正确 echo '数据库连接失败'.mysqli_connect_error(); } //接受注册数据 $name = $_POST['name']; $number = $_POST['number']; $email = $_POST['email']; $money = $_POST['money']; $info = $_POST['info']; $face = imgUpload($_FILES['face']); //SQL语句 $sql = "INSERT INTO student(name,number,email,money,info,face) VALUES('$name','$number','$email','$money','$info','$face')"; $result = $conn->query($sql);//执行注册操作 if (!$result){ echo '注册失败'.mysqli_error($conn);header('refresh:2; url=register.html');//检验注册是否成功 }else{ header('refresh:2; url=index.php'); } //用户名和密码都正确,将用户信息存到Session中 $_SESSION['name'] = $name; $_SESSION['islogin'] = 1; // 处理完附加项后跳转到登录成功的首页 header('location:index.php'); if(!$_POST['sign_out']){ setcookie('name', $name, time()+7*24*60*60); setcookie('code', md5($name.md5($number)), time()+7*24*60*60); }else{ // 删除Cookie setcookie('name', '', time()-999); setcookie('code', '', time()-999); } // 处理完附加项后跳转到登录成功的首页 header('location:index.php'); if($row){//匹配用户名和学号 $_SESSION['name']=$row['name']; $_SESSION['number']=$row['number']; echo "<script>alert('登录成功!2秒后跳转到主页。');location='index.php'</script>"; } out_class.php <?php //搜索 if (!empty($_GET['key'])) { $key = $_GET['key']; $sql = "select id,face,name,info from user where name like '%$key%'"; $stmt = $pdo->query($sql); $user = $stmt->fetchAll(PDO::FETCH_ASSOC); } transfer_class.php <?php require('E:/myphp_www/PHPTutorial/WWW/php_test/imooc_mysql_db/mysqldemo_static/lib/photos.php'); date_default_timezone_set('PRC'); $dsn = "mysql:host=localhost;dbname=imooc_test"; $pdo = new PDO($dsn,'root','root');//PDO方式连接数据库 $moneyinfo = $_POST['moneyinfo']; $sql = "select money from student where id='$moneyinfo'"; $result = $pdo->query($sql); $info = $result->fetch(PDO::FETCH_ASSOC); $pdo -> beginTransaction();//开启事务处理 $sql1 = "update name set money = money+$moneyinfo";//执行sql语句 $r1 = $pdo->exec($sql1); $sql2 = "update name set money = money-$moneyinfo"; $r2 = $pdo->exec($sql2); if($r1>0 && $r2>0){//事务判断 $pdo->commit();//事务提交 echo "<script type='text/JavaScript'>alert('转账成功!');</script>"; }else{ $pdo->rollBack();//事务回滚 echo "<script type='text/JavaScript'>alert('转账失败!');</script>"; } $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);//自动提交 echo "<script type='text/JavaScript'>window.location.href='index.php';</script>";
正在回答
您好,1.首页上显示用户名称时,进行一下判断,存在session值则输出$_SESSION['name'],避免不存在时时出现notice错误。
2.转账操作上的问题:
1)数据库没有设置编码。
2)转账的操作应该是写在if判断中的。当判断是否存在submit后,存在则进行转账。
3)转账操作的时候只获取到了接收金钱的用户,但并没有转账用户的信息。需要在session中获取已经登录账号的信息,因为登录的账号是转账人。
4)在sql语句update中应该添加上where条件,指定哪个用户+了金钱,哪个用户-了金钱。
祝学习愉快!
这是indx.php <?php include('E:/myphp_www/PHPTutorial/WWW/php_test/imooc_mysql_db/mysqldemo_static/lib/photos.php'); header('content-type:text/html;charset=utf8'); session_start();//开启session //连接数据库 $conn = new mysqli('localhost','root','root','imooc_test'); $conn->query('set names utf8');//设定编码方式 if (!$conn){//检查数据库连接是否正确 echo '数据库连接失败'.mysqli_connect_error(); } $sql = "select id,name,number,money,info,face from student"; $result = $conn->query($sql);//执行语句 if ($result){ while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)){ $rows[] = $row; } }else{ die('无法读取数据'.mysqli_error($conn)); } ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>用户转账</title> <link href="style/css/bootstrap.min.css" rel="stylesheet"> <link href="style/css/site.min.css" rel="stylesheet"> <style> .container .row .col-lg-4 img{ display: block; margin-left: auto; margin-right: auto; } .container .row .col-lg-4 h3,p{ text-align: center; } .row .col-lg-4 .button{ width: 360px; margin-left: 150px; margin-bottom: 10px;} </style> </head> <body> <!--导航栏--> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand hidden-sm" href="index.php">慕课网 <?php echo $_SESSION['name']?>,欢迎您</a> </div> <div class="navbar-header" style="display:none;"> <a class="navbar-brand hidden-sm" href=""><?php echo $_SESSION['name']?>,欢迎您!</a> </div> </div> </div> <!--导航栏结束--> <!--巨幕--> <div class="jumbotron masthead"> <div class="container"> <h1>学生转账管理系统</h1> <h2>实现学生转账功能</h2> <p class="masthead-button-links"> <form class="form-inline" action="out_class.php" method="post"> <div class="form-group"> <input type="text" class="form-control" id="exampleInputName2" placeholder="输入搜索内容" name="key" value=""> <button class="btn btn-default" name="search" type="submit">搜索</button> <a href="register.html"><button type="button" class="btn btn-primary btn-default" data-toggle="modal">注册</button></a> <a href="login.html"><button type="button" class="btn btn-primary btn-default" data-toggle="modal">登录</button></a> <a href="login_class.php"><button type="button" class="btn btn-primary btn-default" data-toggle="modal" name="sign_out">退出</button></a> </div> </form> </p> </div> </div> <!--巨幕结束--> <!-- 模态框 --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <form class="form-inline" action="transfer_class.php" method="post" enctype="multipart/form-data"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">转账</h4> </div> <div class="modal-body"> <p> 收款人: <select class="form-control" name="name"> <?php foreach ($rows as $val){?> <option value="<?php echo $val['name'];?>"> <?php echo $val['name'];?></option><?php }?> </select> </p> <br /> <p>转账金额:<input type="text" class="form-control" id="exampleInputEmail1" name="moneyinfo" placeholder="请输入数字"></p> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary" name="submit" id="submit" onclick="show(this)">确认转账</button> <button type="reset" class="btn btn-default">重置</button> </div> </div> </form> </div> </div> <!--模态框结束--> <div class="container projects"> <div class="projects-header page-header"> <h2>用户展示</h2> <p>将用户信息展示在页面中</p> </div> <!--信息展示--> <div class="row"><?php foreach ($rows as $val){?> <div class="col-lg-4"> <img class="img-circle" src="upload/12.jpg" alt="" width="140" height="140"> <h3><?php echo $val['name'];?></h3> <p><?php echo $val['info'];?></p> <div class="button"> <button type="button" class="btn btn-primary btn-default" data-toggle="modal" data-target="#myModal"> 转账 </button> </div> </div><?php }?> <div class="col-lg-4"> </div> </div> </div> <footer class="footer container"> <div class="row footer-bottom"> <ul class="list-inline text-center"> <h4><a href="http://class.imooc.com" target="_blank">class.imooc.com</a> | 慕课网</h4> </ul> </div> </footer> <script src="style/js/jquery.min.js"></script> <script src="style/js/bootstrap.min.js"></script> </body> </html> 这是transfer_class.php <?php include('E:/myphp_www/PHPTutorial/WWW/php_test/imooc_mysql_db/mysqldemo_static/lib/photos.php'); date_default_timezone_set('PRC'); $dsn = "mysql:host=localhost;dbname=imooc_test"; $pdo = new PDO($dsn,'root','root');//PDO方式连接数据库 if (isset($_POST['submit'])){ $moneyinfo = $_POST['moneyinfo']; $name = $_POST['name']; } $pdo -> beginTransaction();//开启事务处理 $sql1 = "update name set money = money+'$moneyinfo'";//执行sql语句 $r1 = $pdo->exec($sql1); $sql2 = "update name set money = money-$moneyinfo"; $r2 = $pdo->exec($sql2); if($r1>0 && $r2>0){//事务判断 $pdo->commit();//事务提交 echo "<script type='text/JavaScript'>alert('转账成功!');</script>"; }else{ $pdo->rollBack();//事务回滚 echo "<script type='text/JavaScript'>alert('转账失败!');</script>"; } $pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);//自动提交 echo "<script type='text/JavaScript'>window.location.href='index.php';</script>";
老师,搜索框发出数据后接收不到,屏幕是白的。 out_class.php <?php //数据库连接及设定字符集 $conn = new mysqli('localhost','root','root','imooc_test'); $conn->query('set names utf8');//设定编码方式 //检测链接 if (!$conn){//检查数据库连接是否正确 echo '数据库连接失败'.mysqli_connect_error(); } //搜索 if (isset($key)) { $key = $_GET['key']; $sql = "select id,name,money,info,face from student where name like '%$key%'"; $stmt = $pdo->query($sql); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($result); } index.php <?php include('E:/myphp_www/PHPTutorial/WWW/php_test/imooc_mysql_db/mysqldemo_static/lib/photos.php'); header('content-type:text/html;charset=utf8'); session_start();//开启session //连接数据库 $conn = new mysqli('localhost','root','root','imooc_test'); $conn->query('set names utf8');//设定编码方式 if (!$conn){//检查数据库连接是否正确 echo '数据库连接失败'.mysqli_connect_error(); } $sql = "select id,name,number,money,info,face from student"; $result = $conn->query($sql);//执行语句 if ($result){ while ($row = mysqli_fetch_array($result,MYSQLI_BOTH)){ $rows[] = $row; } }else{ die('无法读取数据'.mysqli_error($conn)); } ?> <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>用户转账</title> <link href="style/css/bootstrap.min.css" rel="stylesheet"> <link href="style/css/site.min.css" rel="stylesheet"> <style> .container .row .col-lg-4 img{ display: block; margin-left: auto; margin-right: auto; } .container .row .col-lg-4 h3,p{ text-align: center; } .row .col-lg-4 .button{ width: 360px; margin-left: 150px; margin-bottom: 10px;} </style> </head> <body> <!--导航栏--> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> <a class="navbar-brand hidden-sm" href="index.php">慕课网 <?php echo $_SESSION['name']?>,欢迎您</a> </div> <div class="navbar-header" style="display:none;"> <a class="navbar-brand hidden-sm" href=""><?php echo $_SESSION['name']?>,欢迎您!</a> </div> </div> </div> <!--导航栏结束--> <!--巨幕--> <div class="jumbotron masthead"> <div class="container"> <h1>学生转账管理系统</h1> <h2>实现学生转账功能</h2> <p class="masthead-button-links"> <form class="form-inline" action="out_class.php" method="post"> <div class="form-group"> <input type="text" class="form-control" id="exampleInputName2" placeholder="输入搜索内容" name="key" value=""> <button class="btn btn-default" name="key" type="submit">搜索</button> <a href="register.html"><button type="button" class="btn btn-primary btn-default" data-toggle="modal">注册</button></a> <a href="login.html"><button type="button" class="btn btn-primary btn-default" data-toggle="modal">登录</button></a> <a href="login_class.php"><button type="button" class="btn btn-primary btn-default" data-toggle="modal" name="sign_out">退出</button></a> </div> </form> </p> </div> </div> <!--巨幕结束--> <!-- 模态框 --> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <form class="form-inline" action="transfer_class.php" method="post" enctype="multipart/form-data"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">转账</h4> </div> <div class="modal-body"> <p> 收款人: <select class="form-control"> <option value="">user1</option> <option value="">user2</option> <option value="">user3</option> </select> </p> <br /> <p>转账金额:<input type="text" class="form-control" id="exampleInputEmail1" name="moneyinfo" placeholder="请输入数字"></p> </div> <div class="modal-footer"> <button type="submit" class="btn btn-primary" name="submit" id="submit" onclick="show(this)">确认转账</button> <button type="reset" class="btn btn-default">重置</button> </div> </div> </form> </div> </div> <!--模态框结束--> <div class="container projects"> <div class="projects-header page-header"> <h2>用户展示</h2> <p>将用户信息展示在页面中</p> </div> <!--信息展示--> <div class="row"><?php foreach ($rows as $val){?> <div class="col-lg-4"> <img class="img-circle" src="upload/12.jpg" alt="" width="140" height="140"> <h3><?php echo $val['name'];?></h3> <p><?php echo $val['info'];?></p> <div class="button"> <button type="button" class="btn btn-primary btn-default" data-toggle="modal" data-target="#myModal"> 转账 </button> </div> </div><?php }?> <div class="col-lg-4"> </div> </div> </div> <footer class="footer container"> <div class="row footer-bottom"> <ul class="list-inline text-center"> <h4><a href="http://class.imooc.com" target="_blank">class.imooc.com</a> | 慕课网</h4> </ul> </div> </footer> <script src="style/js/jquery.min.js"></script> <script src="style/js/bootstrap.min.js"></script> </body> </html>
您好,同学代码中的思路整体是没有问题的,可以详细对转账做一下判断。
转账:
判断是否点击转账按钮,点击按钮后有弹出框,当点击弹出框上的确认转账按钮时跳转页面,进行转账的操作。
接收表单中关于接收人和金额的信息,从session中获取转账人的信息,然后开始判断。
1)检测转账用户是否已登录,当用户未登录时是不可以转账的,登录时才能获取信息。
2)判断转账人的金额是否大于等于转账的金额,否则输出提示信息。
3)转账时应该判断转账的人与收款的人是否是同一个人,如果时则输出提示信息,不操作转账。
判断完成之后开始则连接数据库,开启事务,进行转账,转账成功输出提示信息跳转首页,转账失败则回滚输出提示信息跳转首页。
搜索:
判断是否点击搜索按钮,点击按钮后接收用户输入的搜索关键字,然后根据关键字从数据库中取出数据,在首页上进行展示。与在首页上展示信息思路相同,不同是要接收关键字,根据关键字查询数据。
祝学习愉快!
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星