为什么赢得变量回声?

时间:2017-07-04 09:40:59

标签: php

我有一个成功页面,在使用Paypal(IPN)成功完成交易后会显示一些变量。

一切都运转良好,但我无法弄清楚为什么$email变量不会响应。查询有问题吗?我已经检查了所有列名称。

我得到了这个输出:

OK
price-1.89

Notice: Undefined variable: payer_email in success.php on line 34
Email-

Success.php

 <?php
        include 'dbConfig.php';

        error_reporting(E_ALL);
        ini_set('display_errors', 1);

        //Get payment information from PayPal
        $item_number= mysqli_real_escape_string($db, $_POST['item_number']);
        $txn_id= mysqli_real_escape_string($db, $_POST['txn_id']);
        $payment_gross= mysqli_real_escape_string($db, $_POST['payment_gross']);




        //Get product price from database
        $productResult = $db->query("SELECT price FROM products WHERE id = '".$item_number."'");
        $productRow = $productResult->fetch_assoc();
        $productPrice = $productRow['price'];

        if(!empty($txn_id) && $payment_gross == $productPrice){

          //Check if payment data exists with the same TXN ID.
            $prevPaymentResult = $db->query("SELECT payment_id FROM payments WHERE txn_id = '".$txn_id."'");

           if($prevPaymentResult->num_rows > 0){

            //Get Email
            $result = $db->query("SELECT payer_email FROM payments WHERE txn_id = '".$txn_id."'");
        $row = $result->fetch_assoc();
        $email=$row['payer_email'];
           }
                echo "OK</br>";
                echo "Price-".$payment_gross."</br>";
                echo "Email-".$email."</br>";

            }else{

              echo '<h1>Error</h1>';
        } 
        ?>

2 个答案:

答案 0 :(得分:1)

你可以改变

$email=$row['payer_email'];

$email=!empty($row['payer_email']) ? $row['payer_email'] : '';

错误显示,因为变量$row没有键payer_email

答案 1 :(得分:0)

在你的代码上:

if (!empty($txn_id) && $payment_gross == $productPrice)
    {

    // Check if payment data exists with the same TXN ID.

    $prevPaymentResult = $db->query("SELECT payment_id FROM payments WHERE txn_id = '" . $txn_id . "'");
    if ($prevPaymentResult->num_rows > 0)
        {

        // Get Email

        $result = $db->query("SELECT payer_email FROM payments WHERE txn_id = '" . $txn_id . "'");
        $row = $result->fetch_assoc();
        $email = $row['payer_email'];
        }

    echo "OK</br>";
    echo "Price-" . $payment_gross . "</br>";
    echo "Email-" . $email . "</br>";
    }
  else
    {
    echo '<h1>Error</h1>';
    }

if ($prevPaymentResult->num_rows > 0)唯一可能暗示这种行为的是不满足这个条件,因此为什么行:

$email = $row['payer_email'];永远不会被处理。