gpt4 book ai didi

php - Issu 必须获取新的交易 ID 一直显示第一个交易 ID

转载 作者:行者123 更新时间:2023-11-29 18:24:28 25 4
gpt4 key购买 nike

我在下新订单时获取新交易 ID 时遇到问题。它只想继续只向我显示用户下的第一个订单。我拥有它,以便用户登录并保留登录 session 。然后用户将商品添加到购物车并结账。数据插入客户订单表后,页面跳转至订单成功页面。唯一的问题是它一直只想显示数据库中该用户的第一个事务 ID (tr_id)。当我使用新用户登录时,我确实得到了不同的 tr_id,但第二个用户也会发生同样的情况。这是我拥有的所有代码。我知道这可能是一个很难解决的问题,如果无法弄清楚,就不要浪费太多时间。如果有必要,我会删除该问题并尝试不同的方法。这是完成我的项目的唯一事情。我也知道这不是最好的代码并且可以获得sql注入(inject)。我想知道获取新交易 ID 时我做错了什么?

main.js

$('#checkout_btn').click(function(){
$.ajax({
url: 'action.php',
method: 'POST',
data: {payment_checkout:1},
success: function(){
window.location.href = "payment_success.php";
}
})
})
cart_checkout();


function cart_checkout()
{
$.ajax({
url: 'action.php',
method: 'POST',
data: {cart_checkout:1},
success: function(data){
$('#cartdetail').html(data);
}
})
}


$("#login").click(function(event){
event.preventDefault();
var email=$('#email').val();
var pwd=$('#password').val();
console.log(pwd);
$.ajax({
url: "login.php",
method: "POST",
data: {userLogin:1,email:email, pwd:pwd},
success: function(data){
if(data=="true"){
window.location.href="profile.php";
}
}
})
})

登录.php

<?php 
include('dbconnect.php');
session_start();

if(isset($_POST['userLogin'])){

$email=mysqli_real_escape_string($conn,$_POST['email']);
$pwd=md5($_POST['pwd']);
$sql="SELECT * FROM user_info WHERE email='$email' AND password='$pwd'";
$run_query=mysqli_query($conn,$sql);
$count=mysqli_num_rows($run_query);

if($count==1){
$row=mysqli_fetch_array($run_query);
$_SESSION['uid']=$row['user_id'];
$_SESSION['uname']=$row['first_name'];
echo "true";
}

}

?>

action.php

if(isset($_POST['cartmenu']) || isset($_POST['cart_checkout']))
{

$uid=$_SESSION['uid'];
$sql="SELECT * FROM cart WHERE user_id='$uid'";
$run_query=mysqli_query($conn,$sql);
$count=mysqli_num_rows($run_query);
if($count>0){
$i=1;
$total_amt=0;
while($row=mysqli_fetch_array($run_query))
{
$sl=$i++;
$pid=$row['p_id'];
$product_image=$row['product_image'];
$product_title=$row['product_title'];
$product_price=$row['price'];
$qty=$row['qty'];
$total=$row['total_amount'];
$price_array=array($total);
$total_sum=array_sum($price_array);
$total_amt+=$total_sum;

if(isset($_POST['cartmenu']))
{
echo "
<div class='row'>
<div class='col-md-3'>$sl</div>
<div class='col-md-3'><img src='assets/prod_images/$product_image' width='60px' height='60px'></div>
<div class='col-md-3'>$product_title</div>
<div class='col-md-3'>$$product_price</div>

</div>
";
}
else
{
echo "
<div class='row'>

<div class='col-md-2'><a href='#' remove_id='$pid' class='btn btn-danger remove'><span class='glyphicon glyphicon-trash'></span></a>
<a href='#' update_id='$pid' class='btn btn-success update'><span class='glyphicon glyphicon-ok-sign'></span></a>
</div>
<div class='col-md-2'><img src='assets/prod_images/$product_image' width='60px' height='60px'></div>
<div class='col-md-2'>$product_title</div>
<div class='col-md-2'><input class='form-control price' type='text' size='10px' pid='$pid' id='price-$pid' value='$product_price' disabled></div>
<div class='col-md-2'><input class='form-control qty' type='text' size='10px' pid='$pid' id='qty-$pid' value='$qty'></div>
<div class='col-md-2'><input class='total form-control price' type='text' size='10px' pid='$pid' id='amt-$pid' value='$total' disabled></div>

</div>
";
}

}







if(isset($_POST['cart_checkout'])){



echo "
<div class='row'>
<div class='col-md-8'></div>
<div class='col-md-4'>
<b>Total: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$$total_amt</b>
</div>
</div>
";
}
}
}

if(isset($_POST['payment_checkout'])){
$uid=$_SESSION['uid'];
$sql="SELECT * FROM cart WHERE user_id='$uid'";
$run_query=mysqli_query($conn,$sql);
$i=rand();

while($cart_row=mysqli_fetch_array($run_query))
{
$cart_prod_id=$cart_row['p_id'];
$cart_prod_title=$cart_row['product_title'];
$cart_qty=$cart_row['qty'];
$cart_price_total=$cart_row['total_amount'];




$sql2="INSERT INTO customer_order (uid,pid,p_name, p_price,p_qty,p_status,tr_id) VALUES ('$uid','$cart_prod_id','$cart_prod_title','$cart_price_total','$cart_qty','CONFIRMED','$i')";
$run_query2=mysqli_query($conn,$sql2);
}

付款成功.php

<?php

include('dbconnect.php');
session_start();

if(!isset($_SESSION['uid'])){
header('Location:index.php');
}


$uid=$_SESSION['uid'];
$sql="SELECT * FROM customer_order WHERE uid='$uid'";
$run_query=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($run_query);
$trid=$row['tr_id'];



?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title> Supplies</title>
<link rel="stylesheet" type="text/css" href="assets/bootstrap-3.3.6-dist/css/bootstrap.css">
<style type="text/css">
.content{
display: none;
}
</style>
</head>
<body>
<div class='content'>
<div class="navbar navbar-default navbar-fixed-top" id="topnav">
<div class="container-fluid">
<div class="navbar-header">
<a href="index.php" class="navbar-brand"> Supplies</a>
</div>


</div>
</div>
<br><br><br><br><br>
<div class='container-fluid'>
<div class='row'>
<div class='col-md-2'></div>
<div class='col-md-8'>
<div class="panel panel-default">
<div class="panel-heading"><h1>Thank you!</h1></div>
<div class="panel-body">
Hello <?php echo $_SESSION['uname']; ?>, your payment is successful.
<br>Your Transaction ID is <?php echo $trid; ?>
<br>You can continue with your shopping.
<p></p>
<a href="profile.php" class='btn btn-success btn-lg'>Back to store</a>
</div>
</div>
<div class='col-md-2'></div>
</div>

</div>

</div>
</div>
<!--Pre-loader -->
<div class="preload"><img src="assets/images/loading.gif" style="width:400px;
height: 400px;
position: relative;
top: 0px;
left: 469px;"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="assets/bootstrap-3.3.6-dist/js/bootstrap.min.js"></script>

<script type="text/javascript">


$(".preload").fadeOut(5000, function(){
$(".content").fadeIn(500);
});

</script>
</body>
</html>

最佳答案

您的 SQL 选择将仅获取第一个 tr-id(事务 id),因为您的选择会创建一个未获取的 tr-id 数组,并按照它们在表中存在的顺序获取它们。

您需要获取结果的最后一个元素。你可以用“foreach”循环来实现(会有更好的方法),它会将每个结果写入变量中,直到最后一个结果保存在变量中。

很可能会有更好的方法,但这也应该有效。

LG! ^^

关于php - Issu 必须获取新的交易 ID 一直显示第一个交易 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46305537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com