Luke G
Luke G

Reputation: 79

Looking into row for data match MySQLi

   <?php
include_once("php_includes/check_login_status.php");
if($user_ok != true){
       header("location: login.php");
    exit();
    }
?>

 <?php  
        $result = mysqli_query($db_conx, "SELECT active FROM profilepage WHERE username='$u'") or die(mysqli_error($db_conx));
        $row = mysqli_fetch_assoc($result) or die ("$u not found"); 
        $a_check = $row['active'];

        if ($u == $log_username && $user_ok == true && $a_check = 1){
            echo include_once("videofeedform.php"); }
            else ($user_ok != true && $a_check = 1 or $user_ok = true && $a_check = 1);
             { echo "viewers should only see this"; }
                exit();

        if($u == $log_username && $user_ok == true && $a_check = 0 or $u != $log_username && $user_ok == true && $a_check = 0 or $u != $log_username && $user_ok != true && $a_check = 0)
        ($a_check = 0); {
            echo ' Video is going to be added here'; }
            exit();

        ?>

ok so basically I am trying to make a module work only if it has been activated to work by the user. Only the owner of the pages sees what they is mean to and the viewer only sees what they are meant to so that part works perfect however i need to tell the script to do these IF's only if the data inside the row match 1 and if they do not match 1 (e.g 0) then they do something else. I believe the current search is only looking for a number of rows called active what is owned by the user and if it is then do...... I was wondering how I would make the query look inside the row to find the data it needs to execute as it just seems like it is just executing when it finds 1 row that matches

Upvotes: 0

Views: 169

Answers (1)

KorreyD
KorreyD

Reputation: 1294

okay there is a decent amount wrong with this script.

you never show us what $log_username or $user_ok so I cannot help to evaluate whether those contain the correct values in your script. We'll just assume they do.


You must brush up on your usage of Comparison Operators in php.:

http://www.php.net/manual/en/language.operators.comparison.php

throughout the conditionals(if, elseif, etc..) in your script you have things like:

if ( $a_check = 1 )

but with a single '=' you are assigning the integer value 1 to the variable $a_check, this is basically the same thing as saying:

if (1)`

which evaluates to true in your if statement (any integer value other than 0 will always evaluate to true).

similarly the line

if ( $a_check = 2 )

is the same as

if (2)

and would also evaluate to true

as aldanux mentioned in his comment above, if you would like to compare if 2 variables are equal you should use ==.

if ( $var1 == $var2 )

if they are equal this will evaluate to true.


More issues with conditionals: http://www.php.net/manual/en/control-structures.elseif.php

this block:

if ($u == $log_username && $user_ok == true && $a_check = 1){
    echo include_once("videofeedform.php"); }
    else ($user_ok != true && $a_check = 1 or $user_ok = true && $a_check = 1);
     { echo "viewers should only see this"; }
    exit();

should be more like:

if ($u == $log_username && $user_ok == true && $a_check == 1){
    echo include_once("videofeedform.php"); 
}

else if ($user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1)
{ 
    echo "viewers should only see this"; 
    exit(); //move exit inside the elseif block
}
  1. fixed conditionals

  2. use elseif since you want to evaluate another expression (e.g. $user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1),

  3. remove semicolon from the end of your else statements

  4. move exit() inside the elseif block (as you currently have the script would always exit on that line).

also an additional comment. the expression:

else if ($user_ok != true && $a_check == 1 or $user_ok == true && $a_check == 1)

is saying i don't care is $user_ok is true or false as long as $a_check equals one. This is the same thing as doing:

else if ($a_check == 1)

though... now im thinking you just want to exit if your previous if statment false:

 if ($u == $log_username && $user_ok == true && $a_check == 1) //if this fails, exit

so your code there would actually look something more like:

if ($u == $log_username && $user_ok == true && $a_check == 1){
    echo include_once("videofeedform.php"); 
}

else{ //just remove the expression from here
    echo "viewers should only see this"; 
    exit(); //move exit inside the elseif block
}

these same issues discussed above exist with your last if statement.

Upvotes: 1

Related Questions