Jump to content

PHP Help


bradymills

Recommended Posts

Hi,

 

It's been a while since I've been here -- Hope you are all well!

 

So, here's my question -- hopefully someone can help -- as I've been at this for hours and hours (days even).

 

Long story short, I'm updating inventory post-payment using Instant Payment Notification from Paypal. IPN is working, as I've set up an email script to confirm that IPN is hitting my .php file. Unfortunately, my database is not updating...

 

I have a feeling it's because of my use of the for loop. IPN sends back data from shopping cart as: item_number1=x, item_number2=x and of course this all depends on the num_cart_items. So, I thought a for loop would work best for inserting this information into the database -- but this could be really wacky code -- and I'm hoping someone can help.

 

Here's the code (all variables are being sent back through POST from Paypal):

<?php
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
//


$num_cart_items = $_POST['num_cart_items'];
require_once ('mysql_connect.php');

for ($x=1; $x <= $num_cart_items; $x++ ) 
{ 
$txn_id = $_POST['txn_id'];
$item_number = $_POST['item_number' . $x .''];
$quantity = $_POST['quantity' . $x . ''];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$payer_email = $_POST['payer_email'];


$query = "INSERT INTO inventory (txn_id, item_number, quantity, first_name, last_name, payer_email, order_date) VALUES ('$txn_id', '$item_number', '$quantity', '$first_name', '$last_name', '$payer_email', NOW())";
$result = @mysql_query ($query); //Run the query.
} 

$body = 'IPN HIT';
mail('myemail@myhost.com', "IPN HIT - POST", $body);
?>

 

Please tell me if I'm a complete idiot.

 

Link to comment
Share on other sites

Please tell me if I'm a complete idiot.

 

Actually it looks good

item_number = $_POST['item_number' . $x .''];
$quantity = $_POST['quantity' . $x . ''];

 

I wudda opted for

item_number = $_POST["item_number$x"];
$quantity = $_POST["quantity$x"];

 

same result :)

 

to save some queries, ya can move the mysql_query outside the loop, and append the query string in the loop

$query .= "INSERT INTO inventory (txn_id, item_number, quantity, first_name, last_name, payer_email, order_date) VALUES ('$txn_id', '$item_number', '$quantity', '$first_name', '$last_name', '$payer_email', NOW());\n";

 

:)

 

Nice job nonetheless

 

Link to comment
Share on other sites

First, thank you for your answers.

 

So I should move this section

$result = @mysql_query ($query); //Run the query.

after the }, correct?

 

so I would end up with

 

$query = blah, blah, blah;

$query = blah, blah, blah;

$query = blah, blah, blah;

etc...

 

$result = blah, blah, blah

 

?

Link to comment
Share on other sites

Thanks to you guys -- and some investigation of my database setup -- It is all working now. If anyone else would like to use this script to process information from IPN, feel free. You, of course, will have to update the variables you want to pull from your IPN response.

 

THANKS!!!! I love this forum.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.