-2

I have a success page that displays some variables after a succesful transaction with Paypal (IPN).

It's all working great but I just can't figure out why the $email variable does not echo out. Is there something wrong with the query? I have triple checked all the column names.

I get this output:

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>';
        } 
        ?>
JulianJ
  • 1,259
  • 3
  • 22
  • 52
  • The query looks good, the problem must be something else. – Troyer Jul 04 '17 at 09:43
  • 1
    You don't have `payer_email` variable in provided code. – u_mulder Jul 04 '17 at 09:43
  • Because it's undefined. I don't see a variable `$payer_email` in your code. – samayo Jul 04 '17 at 09:43
  • Is there something in your "payments" table? – laurent Jul 04 '17 at 09:43
  • Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – samayo Jul 04 '17 at 09:44
  • i think you need to change the variable name in your case `$payer_email=$row['payer_email'];`..Because you are using `$payer_email` which not defined..But you assigned to $email.. – lalithkumar Jul 04 '17 at 09:45
  • Try to change : `"SELECT payer_email FROM payments WHERE txn_id = '".$txn_id."'"` by `"SELECT * FROM payments WHERE txn_id = '".$txn_id."'"` and `var_dump` the `$row` to see what it contains – Esteban Jul 04 '17 at 09:47
  • Also to avoid the error change : `$email=$row['payer_email'];` by `$email = $row['payer_email'] ?: 'N/A';` (but will still throw the `undefined index notice` ) – Esteban Jul 04 '17 at 09:53
  • var_dump($row); shows the column as ["payer_email"]=> string(28) "emailaddress@gmail.com". – JulianJ Jul 04 '17 at 11:11

2 Answers2

1

you can change

$email=$row['payer_email'];

to

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

thia error show because variable $row does not have key payer_email

0

On your code :

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) The only thing that could imply this behaviour is that this condition is not met, hence why the line :

$email = $row['payer_email']; is never processed.

Esteban
  • 1,752
  • 1
  • 8
  • 17