Jump to content

Unwanted duplicate inserts


agravayne

Recommended Posts

Hello All,

 

I have a problem that I am tearing out my hair with. I cannot for the life of me find the source of this problem. I have the code below. It should insert a single row into credit_transaction table and then either insert a new row or update an existing one in indCredits table.

 

It correctly inserts the rows that it is supposed to but in both cases it adds a second row. This second row has lost the MemberID variable (which is a session variable) but the rest of the variables are ok?

 

I also output the sql query to the screen for testing purposes and enetraing the sql directly does not cause this odd behaviour -i.e. I only get the one row.

 

Has anyone got any idea what this could be?

 


<?php

session_start();

$inDate=date("Y-m-d");

$nlink=str_replace("'", "", $_POST['merchant_return_link']);


$sql = mysql_query("INSERT INTO `credit_transaction` (`id`, `from_id`, `to_id`, `date`, `user_id`, `no_of_credits`, `payer_last_name`, `payer_first_name`, `residence_country`, `item_name`, `payment_gross`, `mc_currency`, `business`, `payment_type`, `payer_status`, `verify_sign`, `test_ipn`, `payer_email`, `tax`, `mc_fee`, `payment_fee`, `shipping`, `mc_gross`, `txn_id`, `receiver_email`, `quantity`, `payer_id`, `receiver_id`, `item_number`, `payment_status`, `custom`, `charset`, `notify_version`, `merchant_return_link`) VALUES ('', '0', '$MemberID', '$indate', '$MemberID', '{$_POST['quantity']}', '{$_POST['last_name']}', '{$_POST['first_name']}', '{$_POST['residence_country']}', '{$_POST['item_name']}', '{$_POST['payment_gross']}', '{$_POST['mc_currency']}', '{$_POST['business']}', '{$_POST['payment_type']}', '{$_POST['payer_status']}', '{$_POST['verify_sign']}', '{$_POST['test_ipn']}', '{$_POST['payer_email']}', '{$_POST['tax']}', '{$_POST['mc_fee']}', '{$_POST['payment_fee']}', '{$_POST['shipping']}', '{$_POST['mc_gross']}', '{$_POST['txn_id']}', '{$_POST['receiver_email']}', '{$_POST['quantity']}', '{$_POST['payer_id']}', '{$_POST['receiver_id']}', '{$_POST['item_number']}', '{$_POST['payment_status']}', '{$_POST['custom']}', '{$_POST['charset']}', '{$_POST['notify_version']}', '$nlink')");

echo $sql;

$count=0;
$sql3=mysql_query("select * from indCredits where MemberID=$MemberID");
while( $row = mysql_fetch_array( $sql3 ) )
							{

							$oldcredits=$row['credits'];
							$count++;
							}

							$newcredits=$oldcredits+$_POST['quantity'];	

							if($count=='0')
							{

							$sql2="insert into indCredits (MemberID, credits) values ('$MemberID','$newcredits')";

							$rs2=mysql_query($sql2,$conn);

							}
							else
							{

							$sqlins=mysql_query("update indCredits set credits='$newcredits' where MemberID='$MemberID'");


							}




?> 

Link to comment
Share on other sites

It would be helpful to post your code in the "code" brackets. Like this:

<?php

session_start();

$inDate=date("Y-m-d");
                        
$nlink=str_replace("'", "", $_POST['merchant_return_link']);
                     

$sql = mysql_query("INSERT INTO `credit_transaction` (`id`, `from_id`, `to_id`, `date`, `user_id`, `no_of_credits`, `payer_last_name`, `payer_first_name`, `residence_country`, `item_name`, `payment_gross`, `mc_currency`, `business`, `payment_type`, `payer_status`, `verify_sign`, `test_ipn`, `payer_email`, `tax`, `mc_fee`, `payment_fee`, `shipping`, `mc_gross`, `txn_id`, `receiver_email`, `quantity`, `payer_id`, `receiver_id`, `item_number`, `payment_status`, `custom`, `charset`, `notify_version`, `merchant_return_link`) VALUES ('', '0', '$MemberID', '$indate', '$MemberID', '{$_POST['quantity']}', '{$_POST['last_name']}', '{$_POST['first_name']}', '{$_POST['residence_country']}', '{$_POST['item_name']}', '{$_POST['payment_gross']}', '{$_POST['mc_currency']}', '{$_POST['business']}', '{$_POST['payment_type']}', '{$_POST['payer_status']}', '{$_POST['verify_sign']}', '{$_POST['test_ipn']}', '{$_POST['payer_email']}', '{$_POST['tax']}', '{$_POST['mc_fee']}', '{$_POST['payment_fee']}', '{$_POST['shipping']}', '{$_POST['mc_gross']}', '{$_POST['txn_id']}', '{$_POST['receiver_email']}', '{$_POST['quantity']}', '{$_POST['payer_id']}', '{$_POST['receiver_id']}', '{$_POST['item_number']}', '{$_POST['payment_status']}', '{$_POST['custom']}', '{$_POST['charset']}', '{$_POST['notify_version']}', '$nlink')");

echo $sql;
                        
$count=0;
$sql3=mysql_query("select * from indCredits where MemberID=$MemberID");
while( $row = mysql_fetch_array( $sql3 ) )
                        {
                        
                        $oldcredits=$row['credits'];
                        $count++;
                        }
                           
                        $newcredits=$oldcredits+$_POST['quantity'];   
                           
                        if($count=='0')
                        {
                        
                        $sql2="insert into indCredits (MemberID, credits) values ('$MemberID','$newcredits')";
                  
                        $rs2=mysql_query($sql2,$conn);
                  
                        }
                        else
                        {
                        
                        $sqlins=mysql_query("update indCredits set credits='$newcredits' where MemberID='$MemberID'");
                        
                        
                        }
                        



?>

Link to comment
Share on other sites

Your code is unconditionally executed any time that page is requested (assuming that is all the code on the page.)

 

There are a number of things a browser can do to cause a double-page request and a lot of url rewriting, both correct and incorrect, can cause a url to be requested more than one time.

 

So, you need to find out where the extra empty request for the page is coming from. You also need to add a conditional test to make sure the form was submitted and only execute the database code if it was. Also, setting a session variable that says the data has been processed in that session and checking this to prevent additional inserts after the first one would help.

Link to comment
Share on other sites

mmm I can check the session variable and if its not there prevent a write to the database. That would solve this problem.

 

The original page was far more complicated but what I have posted here is a stripped down version and still has the same problem. Originally I had a query that would check that the $_POST['txn_id'] did not already exist in the database if it did to prevent the insert. However it still wrote twice.

 

I will try checking the session variable first though.

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.