0

Hello I am trying to implement after they pay it checks how much they paid and inputs different variables in sql database with if methods.

The problem: The problem is all the if methods return to this first option(inserts paid=1, and 30days) not sure what the problem is, is my if method broken?what's wrong please explain! thanks!

if(number_format($amount, 2) == 8.00) 
{
    $mysqli = new mysqli(******);
    $stmt = $mysqli->prepare("UPDATE `as_users` SET paid='1', reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
    $stmt->bind_param('s', $username);
    $stmt->execute();
} elseif (number_format($amount, 2) == 10.00) 
{
    $mysqli = new mysqli(******);
    $stmt = $mysqli->prepare("UPDATE `as_users` SET paid='2', reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 30 DAY) WHERE username = ?");
    $stmt->bind_param('s', $username);
    $stmt->execute();
} elseif (number_format($amount, 2) == 100.00) 
{
    $mysqli = new mysqli(******);
    $stmt = $mysqli->prepare("UPDATE `as_users` SET paid='2', reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL 365 DAY) WHERE username = ?");
    $stmt->bind_param('s', $username);
    $stmt->execute();
}

Edit: I tried with $amount and also tried using the names of the array from dropdown menu I have

array("Basic Package-Monthly", "8.00", "Month", "1", "0", "0"),
array("Premium Package-Monthly", "10.00", "Month", "1", "0", "0"),
array("Premium Package-Annually", "100.00", "Year", "1", "0", "0"),
11
  • 2
    You only need the $mysqli = new mysqli(******); once, above the if statement. Commented Jan 23, 2014 at 2:00
  • oh okay thanks robert, do u have any idea to fix the problem tho? Commented Jan 23, 2014 at 2:02
  • Same with number_format($amount, 2). If you put the result of that in a variable, you can just reference that variable in your if statements. It has the added bonus of putting the numeric value in one place so that you can check it. Commented Jan 23, 2014 at 2:03
  • 1
    try to echo out / console log the $amount value before if condition to check what actual value is Commented Jan 23, 2014 at 2:03
  • 1
    You need to clean up the code first, then we can figure out what is wrong with it (assuming the problem doesn't resolve itself naturally when you clean up the code). Commented Jan 23, 2014 at 2:05

2 Answers 2

6

Why worry about the conditional statement, let the switch handle it. Leverage your prepared statement fully.

$mysqli = new mysqli('******');
$paid = false;
$recurring = false;

switch(number_format($amount, 2)):
    case '8.00':
        $paid = 1;
        $recurring = 30;
        break;
    case '10.00':
        $paid = 2;
        $recurring = 30;
        break;
    case '100.00':
        $paid = 2;
        $recurring  = 365;
        break;
endswitch;

$stmt = $mysqli->prepare("UPDATE `as_users` SET paid=?, reg_date=CURRENT_TIMESTAMP, end_date=DATE_ADD(CURRENT_TIMESTAMP(), INTERVAL ? DAY) WHERE username = ?");
$stmt->bind_param('dis', $paid, $recurring, $username);
$stmt->execute();
Sign up to request clarification or add additional context in comments.

7 Comments

smart method, but 2 are monthly and the 100$ version is yearly
I recieved Fatal error: Call to a member function prepare() on a non-object in(the line that starts the prepare statement)
@user3158844 Then your mysqli connection string failed.
oh wow accidently wrote my mysqli password incorrectly hold on fixing
wow it still only fired first one and I payed for the 10$ version hmm
|
1

try to use

number_format($amount, 2, '.', '') instead of number_format($amount, 2) for returning english notation without thousands separator.

i.e

english notation without thousands separator

$number = 1234.56;
$english_format_number = number_format($number, 2, '.', '');

1234.56

and to compare two float values use BC math function

$b = 10.00;
$a = number_format($amount, 2, '.', '');
    bccomp($a, $b)==0  // returns true if both values are 10.00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.