title: How to insert data to database after payment success
body: <p>I want to save the form data to the database after successful payment. I inserted the insert query in between the payment gateway code it stores the data when payment failure also. I am using payumoney gateway. Here is my code please suggest me the solution for this.</p>
  
  <pre><code>&lt;?php
  
  $MERCHANT_KEY = "";
  $SALT = "";
  // Merchant Key and Salt as provided by Payu.
  //$PAYU_BASE_URL = "https://sandboxsecure.payu.in";     // For Sandbox Mode
  $PAYU_BASE_URL = "https://secure.payu.in";          // For Production Mode
  
  $action = '';
  
  $posted = array();
  if(!empty($_POST)) {
      //print_r($_POST);
    foreach($_POST as $key =&gt; $value) {    
      $posted[$key] = $value; 
  
    }
  }
  $formError = 0;
  
  if(empty($posted['txnid'])) {
    // Generate random transaction id
    $txnid = substr(hash('sha256', mt_rand() . microtime()), 0, 20);
  } else {
    $txnid = $posted['txnid'];
  }
  $hash = '';
  // Hash Sequence
  $hashSequence = "key|txnid|amount|productinfo|firstname|email|udf1|udf2|udf3|udf4|udf5|udf6|udf7|udf8|udf9|udf10";
  if(empty($posted['hash']) &amp;&amp; sizeof($posted) &gt; 0) {
    if(
            empty($posted['key'])
            || empty($posted['txnid'])
            || empty($posted['amount'])
            || empty($posted['firstname'])
            || empty($posted['email'])
            || empty($posted['phone'])
            || empty($posted['productinfo'])
            || empty($posted['surl'])
            || empty($posted['furl'])
            || empty($posted['service_provider'])
    ) {
      $formError = 1;
    } else {
      //$posted['productinfo'] = json_encode(json_decode('[{"name":"tutionfee","description":"","value":"500","isRequired":"false"},{"name":"developmentfee","description":"monthly tution fee","value":"1500","isRequired":"false"}]'));
      $hashVarsSeq = explode('|', $hashSequence);
      $hash_string = '';  
      foreach($hashVarsSeq as $hash_var) {
        $hash_string .= isset($posted[$hash_var]) ? $posted[$hash_var] : '';
        $hash_string .= '|';
      }
      $hash_string .= $SALT;
      $hash = strtolower(hash('sha512', $hash_string));
      $action = $PAYU_BASE_URL . '/_payment';
      date_default_timezone_set('Asia/Kolkata');
      $timestamp = date("Y-m-d h:i:s");  
      $conn = mysqli_connect("localhost","root","","hoysalat_temple");
  
      $result= mysqli_query($conn,"INSERT INTO temp_donor_list(`donor_name`, `email`, `donor_no`, `amount`, `date_time`,`cuid`,`web_user_id`) VALUES ('".$posted['firstname']."', '".$posted['email']."', '".$posted['phone']."', '".$posted['amount']."','".$timestamp."','".$uniqeid."','".$uniqeid."')"); 
    }
  } elseif(!empty($posted['hash'])) { 
    $hash = $posted['hash'];
    $action = $PAYU_BASE_URL . '/_payment';
  }
  ?&gt;
  </code></pre>