user3525985
user3525985

Reputation: 109

Is there something wrong with the logic I've applied in this conditional statement?

This is basically part of the paypal IPN , I'm checking to see if the order ID that was sent(txn_id) already exists. If it does I don't want anything to happen, if it doesn't then post to the database. It seems to post to the databse even though it exists, why is it doing that?

$receiver_email = $_POST['receiver_email']; 
$payment_status = $_POST['payment_status'];
$txn_id = $_POST['txn_id'];

$txn_sql =" SELECT * FROM test where item2 = $txn_id ";
$order_num = mysqli_query($Connection, $txn_sql);
$num_rows = mysqli_num_rows($order_num);

if ($num_rows > 0){

  $unique = 'no';
}
else {

  $unique = 'yes';
}



if ($receiver_email == "[email protected]" && $payment_status == "Completed" && $unique =="yes" ) {

$Connection = mysqli_connect("localhost", "user", "pw", "db");
// Check connection
if (mysqli_connect_errno()) {
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

 $sql="INSERT INTO test (item1, item2)
 VALUES ('$txn_id', '$unique')";

 $do = mysqli_query($Connection, $sql);

I get, the value I sent along with "yes" for item2 all the time, shouldn't it not post at all if it exists? Where am I going wrong?

Upvotes: 0

Views: 47

Answers (1)

lodev09
lodev09

Reputation: 442

First, make sure item2 field is an integer since you are comparing without '. Next, try to var_dump($num_rows) and see the real result or even var_dump($_POST).

If it is really > 1, try to echo $txn_sql, copy the sql statement and run it to your actual database (using navicat or something) and see if you have problem with your database.

It may help to turn on your display_errors option in php.ini or ini_set('display_errors", "on") to check if you have connection issues or other errors.

I'm just answering this to help you debug your problem. Hope that helps :)

Upvotes: 1

Related Questions