Can't get my final if statement to work


#1

I have almost got my website working but just a bit stuck with my final if statement and according to php manual, I can do something like this...

In this instance, the 'else if' is a shorthand/inline else statement (no curly braces) with the if statement as a body. It is the same things as:

<?php
  if($var == 'Whatever') {

  } else {
      if($var == 'Something Else') {

      }
  }

I guess I am missing some brackets or braces at the end but just can't work out where exactly..

<!DOCTYPE html>
<html>
<head>
   <title></title>
   <meta charset="utf-8">
   <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>


<?php

session_start();

if(!isset($_SESSION['u_uid'])) {
  header("Location: index.php?notlevel1user");
  exit();
} else {
  include_once 'includes/dbh.php';

                           
             $sql = "SELECT subscriptionplan, subscriptionplan2, subscriptionplan3, subscriptionplandate, subscriptionplandate2, subscriptionplandate3, fees, fees2, fees3, totalfees, paid, paid2, paid3, expirydate, expirydate2, expirydate3, paidbydate, paidbydate2, paidbydate3, emailreminder, emailreminder2, emailreminder3, overdue, overdue2, overdue3, activate, activate2, activate3 FROM memberships WHERE user_uid = ?";
                            
                 // Create a prepared statement
                  $stmt = mysqli_stmt_init($conn);
                 //Prepare the prepared stement

                 if (!mysqli_stmt_prepare($stmt, $sql)) {
                     echo "SQL statement failed";

                  } else {
                     //Bind parameters to the placeholder
                     mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
                     mysqli_stmt_execute($stmt);
                     mysqli_stmt_bind_result($stmt, $subscriptionplan, $subscriptionplan2, $subscriptionplan3, $subscriptionplandate, $subscriptionplandate2, $subscriptionplandate3, $fees, $fees2, $fees3, $totalfees, $paid, $paid2, $paid3, $expirydate, $expirydate2, $expirydate3, $paidbydate, $paidbydate2, $paidbydate3, $emailreminder, $emailreminder2, $emailreminder3, $overdue, $overdue2, $overdue3, $activate, $activate2, $activate3);

                      while(mysqli_stmt_fetch($stmt)) {
                      if ($subscriptionplan !== 'None' && $activate == 0) {
                          header("Location: index.php?level1=notactivated");
                          exit();

                       } else {
                            if ($subscriptionplan === 'Level 1' && date("Y-m-d") > $paidbydate && $activate == 1) {
                                          
                               $sql = "UPDATE memberships
                                          SET paidbydate = now() + interval '2' day, emailreminder = 1, overdue = 1
                                          WHERE user_uid = ?;

                                                 ";

                                          $stmt = mysqli_stmt_init($conn);
                                          //Prepare the prepared stement
                                          if (!mysqli_stmt_prepare($stmt, $sql)) {
                                             echo "SQL statement failed";

                                             } else {

                                            //Bind parameters to the placeholder
                                            mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
                                             mysqli_stmt_execute($stmt);
                                             header("Location: index.php?level1=overdue");
                                           
                                         
                                           if($subscriptionplan === 'Level 1' && date("Y-m-d") > $paidbydate && $activate == 1 && $emailreminder == 1) {

                                              $sql = "UPDATE memberships
                                                      SET subscriptionplan = '', paidbydate = '', expirydate = '', fees = '', totalfees = '', emailreminder = 0, activate = 0, overdue = 0, paid = ''
                                                      WHERE user_uid = ?;

                                                     ";

                                              $stmt = mysqli_stmt_init($conn);
                                              //Prepare the prepared stement
                                              if (!mysqli_stmt_prepare($stmt, $sql)) {
                                                 echo "SQL statement failed";

                                             } else {
                                            //Bind parameters to the placeholder
                                             mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
                                             mysqli_stmt_execute($stmt);
                                             header("Location: index.php?level1=cancel");
                                            
                                             
                                             if ($subscriptionplan === 'Level 1' && date("Y-m-d") < $paidbydate && $paid == 1 && $activate == 1) {
                                                header("Location: level1videos.php");
                                                exit();
                                           } else {
                                                header("Location: index.php?level1=error");
                                                exit();
                                           }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }

#2

Are you actually getting an error, or is it just not doing what you expect it to do?


#3

Hi! I am not getting any errors but it is nit executing the last if statement. . Just stucked on cancel and just keep looping through that sql query.. i just think i need to get out of the query.. i think my else } is not closed off before my binding statement, is that correct?


#4

Have you setup error_reporting() and requested errors and warnings to be displayed which has surely been previously mentioned.


#5

Nope. If the else wasn't closed, you'd get an error. My guess is one of your values are not what you think they are (though why do you have these in here twice? If those first three conditions weren't true at the first if, it wouldn't reach the second.

if ($subscriptionplan === 'Level 1' && date("Y-m-d") > $paidbydate && $activate == 1) {

if($subscriptionplan === 'Level 1' && date("Y-m-d") > $paidbydate && $activate == 1 && $emailreminder == 1) {

#6

The 1st if statement is meant to send the user an email to remind them them that they haven't paid and hence extends their pay by date to anotger 2 days, and if they have already received the email, it should cancel their account the next time but those are working fine... it is my last if statement..... maybe it is not recognising the paid variable....

I am new to prepare statement but can i bind any values to match the select statement? Instead of $subscriptionplan, can i call it $_session ['subscriptionplan']

I will try setting error reporting here and see but have done that for some of my other pages.. thanks guys!


#8

I see you still have not listened to the expert advice given to you to fix your database FIRST. Until you do that you are just wasting our (and your) time.


#9

Error reporting is not something you just try when you get stuck. It is an essential tool during the development process.
Always have error reporting on during development and error logging on the finished live site.
Suddenly, things you are doing wrong become apparent. For example, when one of your header redirects runs:-

header("Location: index.php?level1=notactivated");

...you will get a headers already sent error. Something you are blissfully unaware of presently. But since a header redirect seems to be the only outcome of the script, you could probably just remove the redundant html from the beginning of the script.


#10

Your final if() statement is immediately preceded by a header redirect, so once you get header redirects working (i.e. stop sending output before them), it won't execute anyway.

 header("Location: index.php?level1=cancel");
 if ($subscriptionplan === 'Level 1' && date("Y-m-d") < $paidbydate && $paid == 1 && $activate == 1) {

I'm also a bit surprised at what is working. Inside a loop where you are retrieving data using $stmt as the results object, you then run another query on yet another version of $stmt:

while(mysqli_stmt_fetch($stmt)) {
... then after a few lines of code
 } else {
  if ($subscriptionplan === 'Level 1' && date("Y-m-d") > $paidbydate && $activate == 1) {
                                          
    $sql = "UPDATE memberships
       SET paidbydate = now() + interval '2' day, emailreminder = 1, overdue = 1
       WHERE user_uid = ?;
        ";

     $stmt = mysqli_stmt_init($conn);

Surely that will give you problems? Unless you're only ever expecting to retrieve one row from the first query, in which case the while() loop isn't needed.


#11

I was going to say the same thing, but testing, if you have a redirect header, any code that follows is processed. If that following code contains another redirect header, you get sent to the second location.
My main concern with that if condition is that if not met, nothing happens, there is no else to catch it.
That and two of the conditions have already been tested for, so could be a fraction of the length.

if($emailreminder == 1){
}
else{
     // What now?
}

Yes. And if you really do need a while (which I don't think you do) and run queries within it, it pays to prepare your statements before entering the loop, and only bind and execute inside the loop.
One of the main points of prepared statements is re-usability to make the app more efficient.


#12

That's true. I just didn't notice that there wasn't an exit() after it.


#13

That many nested conditionals is a sign of very low quality code. It might work but that is a gigantic mess. Lack of separation of concerns in and out of php in a template that has a bunch of business logic, exit everywhere… It doesn't get much worse than what you have built here. The is one of the many reasons I'm a proponent of frameworks. You can't do things this sloppy in a good framework. People say they learn better making things from scratch. I don't see much learning here at all. If anything I just see a big gigantic mess of low quality code.


#14

@TenDoLLA went above and beyond and wrote a complete solution correcting all the OP's flawed DB design and code yet OP continues with this junk. There is just no excuse at this point.

TenDoLLA code


#15

I tried to do it with a while loop but it didn't work and i read in the php manual that i need the while loop to fetch all object or i can't echo anything on the screen... i guess what you are trying to say is to use the while loop only for echoing out data otherwise don't use it?

But i think someone was spot on about how it is my if and else problem not processed.. maybe i should put the last statement at the very top? But that would just be a quick fix.. i am sure there is a way around it


#16

Not sure who actually authored that, but the == 1 is redundant and completely unnecessary. The if is a boolean check and already evaluates true or false.


#17

I have read this from stackflow and they mentioned that you need to use exit ()

After the header redirect command you need to exit; otherwise the code just continues to run, giving duplicate header commands - the last one you send is the one that acts.

I think my problem might be either the header or exit but when i added the exit, it won't execute the next statement but i will check all my variables again...


#19

Try setting a breakpoint and examine the variables:

  while($row = mysqli_stmt_fetch($stmt))
  {
    // BREAKPOINT
       echo '<br>' .__LINE__; print_r($row); die;

Once you know the returned values then move the break point to the next place where you check for particular values.


#20

Do i need to put everything else after that in /* */ so that it won't run? I see.. the die () will kill the script


#21

I am not sure what you mean.

The break point is to be added to your existing script and yes die; is virtually the same as exit; only easier to type.

As mentioned once the variables are what you expect then move the line to inspect other variables otherwise correct the problem, check again then move on.

I have a function which does the same and is easier to implement:

/*
  usage:
    fred($var, $title); die;
*/
//===================
function fred($var=NULL, $title=NULL)
{
  echo '<br> $title ==> ' .$title;
  echo  '<pre>'; // pretty formatting
    print_r($val);
  echo '</pre>';
}///

#22

With regard to prepare statement and doing the mysqli_stmt_bind_result ()... can i use session?