简体   繁体   English

IPN的PHP自定义变量

[英]PHP Custom Variables For IPN

Ok so I'm sure a lot of people have already asked this, yet I have not seen any real clear answers. 好的,所以我确定很多人已经问过这个问题,但是我还没有看到任何真正明确的答案。 I need to capture a variable that I already have a value for....here is my example: 我需要捕获一个已经具有值的变量。...这是我的示例:

<?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
}

This shows as this: 如下所示:

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

The line with all of the * * in it is where I'm having trouble. 我遇到麻烦的地方是带有所有* *的那一行。

I created the button in paypal.com but I added the * line in myself. 我在paypal.com中创建了按钮,但我自己添加了*行。

I need this variable to be passed to paypal so that I when my database is updated through ipn.php. 我需要将此变量传递给paypal,以便在通过ipn.php更新数据库时使用。 Here is the MYSQL update area for 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')";

But for some reason even though I added the $dream_id = $_POST['dream_id'] and also added $dream_id into the INSERT part of the mysql insert, it doesn't insert the actual variable, it just makes it 0. 但是由于某种原因,即使我添加了$ dream_id = $ _POST ['dream_id']并将$ dream_id添加到mysql插入的INSERT部分中,它也没有插入实际变量,只是使其变为0。

I hope I made this clear enough for everyone, could I please get a bit of help on how to make this work properly. 我希望我对每个人都说得足够清楚,请让我对如何使其正常工作有所帮助。

Change 更改

$dream_id = $_POST['dream_id'];

to

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

If you look closely (and if I remember correctly) there's a dot at the end of PayPal's custom value field. 如果您仔细观察(如果我没记错的话),在PayPal的自定义值字段的末尾会出现一个点。 Typecasting the variable to an integer fixes this problem as well as protect against SQL injection . 将变量类型转换为整数可以解决此问题并防止SQL注入 You can read more on typecasting in PHP here . 您可以在此处阅读有关PHP中的类型转换的更多信息

Got it working everyone....ok so here is how its done: 让每个人都能正常工作....好,这是完成的过程:

Here is our button script without the added 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="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>

Add this line into our form.... 将此行添加到我们的表单中。

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

Replace the $MY_VARIABLE with your own variable. 将$ MY_VARIABLE替换为您自己的变量。

Our new code for our button will look like 按钮的新代码如下所示

    <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>

After that, in your ipn.php change this area 之后,在您的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')";

Add these into it (I'm using an integer so I'm just making sure its the right type) 将它们添加到其中(我使用的是整数,所以我只是确保其类型正确)

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

and

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

After all of our fixes, this entire area should look like this: 完成所有修复后,整个区域应如下所示:

    // 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')";

I hope this all helps, thank you for your time. 希望对您有所帮助,谢谢您的宝贵时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM