title: Paypal IPN listener page inserting MySQL data multiple times
body: <p>I have been experimenting all day with the PayPal IPN listener in sandbox mode, trying to adapt the sample code for my own use. I am confused with the results I am getting - the MySQL query to be run at the bottom if inserting 6 lines of data for each IPN sent to the listener script. I've changed the query and have reproduced a typical insert to the database to illustrate my point - see the table below the code. Any ideas? I'm sure I remember this happening before with some other API but sure it will be something fundamental causing it. I will of course escape the data properly once I can verify that the form is working.</p>
  
  <pre><code>&lt;?php
  $dbc = mysqli_connect ('host', 'user', 'pass', 'db');
  
  // read the post from PayPal system and add 'cmd'
  $req = 'cmd=_notify-validate';
  foreach ($_POST as $key =&gt; $value) {
  $value = urlencode(stripslashes($value));
  $req .= "&amp;$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);
  
  
  if (!$fp) {
  // HTTP ERROR
  } else {
  fputs ($fp, $header . $req);
  while (!feof($fp)) {
  $res = fgets ($fp, 1024);
  if ((stristr ($res, "VERIFIED") == 0) &amp;&amp;
  ($_POST['payment_status'] == 'Completed'))
  {
  
  // PAYMENT VALIDATED &amp; VERIFIED!
  $email = $_POST['payer_email'];
  
  $next = mysqli_query($dbc, "INSERT INTO users (email, password) VALUES('$email', NOW() ) ");  
  
  $to      = $email;
  $subject = 'Download Area | Login credentials';
  $message = '
  
  Thank you for your purchase
  
  Your account information
  -------------------------
  Email: '.$email.'
  Password: 
  -------------------------
  
  You can now login at http://yourwebsite.com/PayPal/';
  $headers = 'From:noreply@downloadarea.com' . "\r\n";
  
  mail($to, $subject, $message, $headers);
  
  }
  </code></pre>
  
  <p>The data I got from one IPN is as follows:</p>
  
  <pre><code>+----+-------------------+---------------------+
  | id | email             | password            |
  +----+-------------------+---------------------+
  | 25 | testing@gmail.com | 2013-07-03 17:15:58 |
  +----+-------------------+---------------------+
  | 26 | testing@gmail.com | 2013-07-03 17:15:58 |
  +----+-------------------+---------------------+
  | 27 | testing@gmail.com | 2013-07-03 17:15:58 |
  +----+-------------------+---------------------+
  | 28 | testing@gmail.com | 2013-07-03 17:15:58 |
  +----+-------------------+---------------------+
  | 29 | testing@gmail.com | 2013-07-03 17:15:59 |
  +----+-------------------+---------------------+
  | 30 | testing@gmail.com | 2013-07-03 17:15:59 |
  +----+-------------------+---------------------+
  </code></pre>
  
  <p>Why is data being inserted multiple times for this IPN?</p>