仅在while循环中获取最后一个Transaction ID信息时出现问题

时间:2017-09-19 20:54:34

标签: php mysql mysqli

我遇到一个问题,即用户ID的数据正在抓取在该用户ID下输入的所有数据。我只需要使用相同的tr_id获取输入数据库的当前数据,而不是用户在表中使用的所有数据。

 example 

***How I want it to show.*** 
    tr_id 1234 product:pens qty:2
    tr_id 1234 product:pencils qty:2

***not Like this*** 
    tr_id 1234 product:pens    qty:2
    tr_id 1234 product:pencils qty:2
    tr_id 3456 product:papers  qty:2
    tr_id 3456 product:markers qty:2

test.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' order by id desc";
        $run_query=mysqli_query($conn,$sql);

      while ($row = mysqli_fetch_array($run_query))
    {
         echo $row['pid'] . " " . $row['p_name'] . " " . $row['tr_id'] . "<br/>";
    }


    ?>

action.php的

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);
        }

1 个答案:

答案 0 :(得分:0)

麦克雷德评论引导我得到这个答案

<?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'   order by id desc";
        $run_query=mysqli_query($conn,$sql);
        $row = mysqli_fetch_array($run_query);

        $tr_id = $row['tr_id'];

        $sql="SELECT * FROM customer_order WHERE uid='$uid' and tr_id='$tr_id'  order by id desc";
        $run_query=mysqli_query($conn,$sql);

      while ($row = mysqli_fetch_array($run_query))
    {
         echo $row['pid'] . " " . $row['p_name'] . " " . $row['tr_id'] . "<br/>";
    }


    ?>