可以提供转账页面的代码吗?我接收不到收款人的信息
转账界面
<?php
//判断是否登录,如未登录则提示
session_start();
if(!isset($_SESSION['name'])||!isset($_SESSION['number'])){
echo "<script>alert('请登录!!!');
window.location.href='login.php';
</script>";
}
$name1=$_SESSION['name'];//付款人
$id2=$_POST['receiver'];//收款人
$tomoney=$_POST['tomoney'];//转账金额
//转账页
header("content-type:text/html;charset=utf-8");
error_reporting(E_ALL ^ E_NOTICE);
$dsn="mysql:host=localhost;dbname=users";
$pdo=new PDO($dsn,'root','');
$pdo->exec("set names utf8");
$sql3="SELECT * FROM users";
$smtm=$pdo->query($sql3);
$data=$smtm->fetchall(PDO::FETCH_ASSOC);
$pdo->beginTransaction();//开启事务
//转账 事务
$sql1="UPDATE users SET money=money-{$tomoney} WHERE name={$name1}";
$r1=$pdo->query($sql1);
$sql2="UPDATE users SET money=money+{$tomoney} WHERE id={$id2}";
$r2=$pdo->exec($sql2);
//判定是否成功
if($r1>0&&$r2>0){
$pdo->commit();
echo "转账成功";
}else{
$pdo->rollback();
echo "转账失败";
}
$pdo->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
?>
这是index界面
<!-- 模态框 -->
<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" name='zz'>转账</h4>
</div>
<div class="modal-body">
<p>
收款人:
<select class="form-control" name="receiver">
<?php foreach ($data as $key=>$student):?>
<option value="<?php echo $key;?>">
<?php echo $student['name'];?>
</option>
<?php endforeach;?>
</select>
</p>
<br />
<p>转账金额:<input type="text" class="form-control" id="exampleInputEmail1" name= "tomoney" 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>
<!--模态框结束-->
总是失败!请给出正确代码及解释,谢谢
正在回答 回答被采纳积分+1
你好,你的index页面的模态框遍历用户出现了问题,用户有很多个,代码中的$key获取到的是每一条用户数据的下标,而不是每一条用户数据的id字段值,以下两个页面代码供您参考。
index页面:
<?php
session_start();
date_default_timezone_set('PRC');
if(isset($_SESSION['id'])){
$from = $_SESSION['id'];
}else{
$from = '';
}
require('./lib/func.php');
//连接数据库
$pdo = mysqlInit('localhost', 'root', 'root', 'user_money');
//搜索
$get=$_GET;
if(!empty($get['key'])){
$key=$get['key'];
$sql = "select id,name,info,face from m_user where name like '%$key%' order by id desc";
}else{
$key = '';
$sql = "select id,name,info,face from m_user order by id desc";
}
$result=$pdo->query($sql);
$data = $result -> fetchall(PDO::FETCH_ASSOC);
$sql = "select id,name from m_user order by id desc";
$result=$pdo->query($sql);
$user = $result -> fetchall(PDO::FETCH_ASSOC);
?>
<!-- 模态框 -->
<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="touser">
<?php
foreach ($user as $user){
?>
<option value="<?php echo $user['id'] ?>" ><?php echo $user['name']; ?></option>
<?php
}
?>
</select>
</p>
<br />
<p>转账金额:<input type="text" class="form-control" name="money" placeholder="请输入数字"></p>
<input type="hidden" name="fromuser" value="<?php echo $from;?>"/>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" id="submit">确认转账</button>
<button type="reset" class="btn btn-default">重置</button>
</div>
</div>
</form>
</div>
</div>
<!--模态框结束-->
转账处理页面:
?php
require './lib/func.php';
date_default_timezone_set('PRC');
$touser=$_POST['touser'];
$fromuser = $_POST['fromuser'];
$money = $_POST['money'];
if($touser == $fromuser || $fromuser == ''){
echo "<script type='text/JavaScript'>alert('转账信息错误!');</script>";
echo "<script type='text/JavaScript'>window.location.href='index.php';</script>";
return;
}
//连接数据库
$pdo = mysqlInit('localhost', 'root', 'root', 'user_money');
$sql = "select money from m_user where id='".$fromuser."'";
$result = $pdo->query($sql);
$userinfo = $result->fetch(PDO::FETCH_ASSOC);
if($userinfo['money'] <= $money){
echo "<script type='text/JavaScript'>alert('金额不足,请重新调整转账金额!');</script>";
echo "<script type='text/JavaScript'>window.location.href='index.php';</script>";
return;
}
//开启事务处理
$pdo -> beginTransaction();
//执行sql语句
$sql1 = "update m_user set money = money+$money where id = $touser";
$r1 = $pdo->exec($sql1);
$sql2 = "update m_user set money = money-$money where id = $fromuser";
$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>";
如果解决了您的问题,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 225 份
- 解答问题 3372 个
掌握用PHP开发互联网网站的必备功能,掌握当下主流的Linux系统开发,并熟练使用热门框架ThinkPhp开发电商团购项目,是通向PHP工程师必经之路。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星