IPN的PHP自定义变量


PHP Custom Variables For IPN

好的,我相信很多人已经问过了,但我还没有看到任何真正明确的答案。我需要捕获一个已经有值的变量。。。。这是我的例子:

<?php
$data_p = mysql_query("SELECT * FROM `dreams`") or die(mysql_error());
while($info = mysql_fetch_array( $data_p )) {
    <table>
        <tr>
            <td><?php echo $info['first']; ?> <?php echo $info['last']; ?></td>
            <td><?php echo $info['city']; ?> <?php echo $info['state']; ?></td>
            <td><?php echo $info['dream']; ?></td>
            <td>$<?php echo $info['donation_goal']; ?></td>
            <td>
                <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
                <input type="hidden" name="cmd" value="_s-xclick">
                <input type="hidden" name="hosted_button_id" value="2HGLK2WGELUNG">
****************<input type="hidden" name="dream_id" value="<?php echo $info['dream_id']; ?>">
                <input type="image" src="images/donatebutton.png" id="donate" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
                </form>
            </td>
        </tr>
    </table
}

显示如下:

http://www.dreamsurreal.com/images/example.jpg

其中包含所有**的行是我遇到麻烦的地方。

我在paypal.com中创建了按钮,但我自己添加了*行。

我需要将这个变量传递给paypal,以便在我的数据库通过ipn.php更新时使用。以下是ipn.php的MYSQL更新区域:

// add this order to a table of completed orders
    $payer_email = mysql_real_escape_string($_POST['payer_email']);
    $mc_gross = mysql_real_escape_string($_POST['mc_gross']);
    $dream_id = $_POST['dream_id'];
    $sql = "INSERT INTO orders VALUES 
            (NULL, '$txn_id', '$payer_email', '$mc_gross', '$dream_id')";

但出于某种原因,尽管我添加了$dream_id=$_POST['dream_id'],并在mysql插入的INSERT部分中添加了$dream_id,但它并没有插入实际的变量,只是将其设为0。

我希望我能让每个人都清楚地知道这一点,我能得到一些帮助吗。

更改

$dream_id = $_POST['dream_id'];

$dream_id = (int)$_POST['dream_id'];

如果你仔细看(如果我没记错的话),PayPal的自定义值字段末尾有一个点。将变量类型转换为整数可以修复此问题,并防止SQL注入。您可以在这里阅读更多关于PHP中的类型转换的信息。

让每个人都能工作。。。。好的,下面是它的做法:

这是我们的按钮脚本,没有添加变量。。。。

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" value="2HGLK2WGELUNG">
    <input type="image" src="images/donatebutton.png" id="donate" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </form>

将此行添加到我们的表单中…

<input type="hidden" name="item_number" value="<?php echo $MY_VARIABLE; ?>">

将$MY_VARIABLE替换为您自己的变量。

我们按钮的新代码将看起来像

    <form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
    <input type="hidden" name="cmd" value="_s-xclick">
    <input type="hidden" name="hosted_button_id" value="2HGLK2WGELUNG">
    <input type="hidden" name="item_number" value="<?php echo $MY_VARIABLE; ?>">
    <input type="image" src="images/donatebutton.png" id="donate" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
    <img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
    </form>

之后,在您的ipn.php中更改此区域

// add this order to a table of completed orders
    $payer_email = mysql_real_escape_string($_POST['payer_email']);
    $mc_gross = mysql_real_escape_string($_POST['mc_gross']);
    $sql = "INSERT INTO orders VALUES 
            (NULL, '$txn_id', '$payer_email', '$mc_gross')";

将这些添加到其中(我使用的是一个整数,所以我只是确保它是正确的类型)

$MY_VARIABLE = (int)$_POST['item_number'];

$sql = "INSERT INTO orders VALUES 
    (NULL, '$txn_id', '$payer_email', '$mc_gross', '$MY_VARIABLE')";

在我们所有的修复之后,整个区域应该是这样的:

    // add this order to a table of completed orders
    $payer_email = mysql_real_escape_string($_POST['payer_email']);
    $mc_gross = mysql_real_escape_string($_POST['mc_gross']);
    $dream_id = (int)$_POST['item_number'];
    $sql = "INSERT INTO orders VALUES 
            (NULL, '$txn_id', '$payer_email', '$mc_gross', '$dream_id')";

我希望这一切都有帮助,谢谢你抽出时间。