0

SO i WAS TESTING my code..and it doesnt seem to work...here it is

<?php
require_once('../includes/config.php');
if ($_Session['roleID']=1) {
                    echo "admin";
                }           
                elseif ($_Session['roleID']=2) {
                    echo "faculty";
                }


?>
6
Contributors
35
Replies
91
Views
3 Years
Discussion Span
Last Post by RonKevinT.Manuela
Featured Replies
  • Try starting the session at the top `<?php session_start(); ?>` And **$_SESSION** should be in uppercase Read More

  • 1
    cereal 1,419   3 Years Ago

    The variable session must be always uppercase, so `$_Session` is not correct, change it to `$_SESSION`: $_SESSION['roleID'] The `session_start();` is included in the config file? Also, in the `IF` statement you have to compare, not assign a value, so this: if ($_Session['roleID']=1) Becomes: if ($_Session['roleID'] == 1) When you're testing … Read More

  • 1
    cereal 1,419   3 Years Ago

    I made a mistake in my previous post, this: if ($_Session['roleID'] == 1) Should be: if ($_SESSION['roleID'] == 1) Uppercase! About this: `Parse error: syntax error, unexpected 'if' (T_IF) in...` read the full error code. It can happen because of a missing `;`, for example: <?php echo 'hello' if(1 == … Read More

  • 1

    Line 4 uses a single quoted string., You cannot put a variable in it, use a double quoted string: $roleID = ("SELECT roleID from blog_members where roleID={$_SESSION['roleID']}"); Next problem is that `$roleID` is just a string, you need to execute the query. Read More

  • 1

    http://www.daniweb.com/web-development/php/code/_/17 I wrote some snippets using PDO and/or MySQLi (on page 1 and 2). Use one of those. Read More

1

Try starting the session at the top
<?php session_start(); ?>

And $_SESSION should be in uppercase

Edited by niranga: Added more information

1

The variable session must be always uppercase, so $_Session is not correct, change it to $_SESSION:

$_SESSION['roleID']

The session_start(); is included in the config file?

Also, in the IF statement you have to compare, not assign a value, so this:

if ($_Session['roleID']=1)

Becomes:

if ($_Session['roleID'] == 1)

When you're testing booleans, instead, use === as explained here:

Apply the same to the ELSEIF statement.

0

Oh so it sould be like == not =.....i see thanks but i get an error dude..it says

Parse error: syntax error, unexpected 'if' (T_IF) in....

Edited by RonKevinT.Manuela: wrong

0

Are you getting an error even after applying the changes that cereal mentioned? or Does $_SESSION['roleID'] contain a value other than 1 or 2?

0

the main point here is that I retrieve the roleid of the currently logged in user....its just a testing code so it doesnt seem that way..did I use the right method to retrieve roleID from my database?$_Session['roleid']?..

1

I made a mistake in my previous post, this:

if ($_Session['roleID'] == 1)

Should be:

if ($_SESSION['roleID'] == 1)

Uppercase! About this: Parse error: syntax error, unexpected 'if' (T_IF) in... read the full error code. It can happen because of a missing ;, for example:

<?php

echo 'hello'

if(1 == 1) echo ' world';

Will return: Parse error: syntax error, unexpected T_IF, expecting ',' or ';' in .... So check if the code in the config file is correct.

If you want to check the session status just try this in a test page:

<?php

    session_start();

    echo "<pre>";
    print_r($_SESSION);
    echo "</pre>";

It will return all the values saved in the current session.

0

forgot a semicolon but now it shows.." Notice: Undefined index: roleID in C:\wamp\www\simpleblog\admin\menu.php on line 5 and 8...

0

unfortunately this gives an error..i just want to store the roleid of the current logged in user to $roleid so my if statement would work...Parse error: syntax error, unexpected 'roleID' (T_STRING) in C:\wamp\www\simpleblog\admin\menu.php on line 4

<?php

include_once('../includes/config.php');
$roleID=('SELECT roleID from blog_members where roleID=$_SESSION['roleID']');

if ($roleID == 1) {
                    echo "admin";
                }           
                elseif ($roleID == 2 ) {
                    echo "faculty";
                }


?>

Edited by RonKevinT.Manuela: wrong

1

Line 4 uses a single quoted string., You cannot put a variable in it, use a double quoted string:

$roleID = ("SELECT roleID from blog_members where roleID={$_SESSION['roleID']}");

Next problem is that $roleID is just a string, you need to execute the query.

Edited by pritaeas

0

thanks but what the hell is this? i cant fix it.
Notice: Undefined index: roleID in C:\wamp\www\simpleblog\admin\menu.php on line 4...

0

thats impossible roleid in blog_members table has a value..currently i have 2 users...one having a roleid of 1 and the other having a roleid of 2.....hmmmm...

0

Pritaeas-sensei, please help...note: im an effin beginner..just started playing with php last week

0

First try Disabling the error reporting first, by adding;

error_reporting(0);

at the top of your page.
Let us know whether it works or not.

0

thats impossible roleid in blog_members table has a value

That it exists in your table, does not automatically mean it exists in your session. Do you have code to retrieve the role from the table? If so, show that piece.

0

I tried to look at some tutorials...let me know if im getting close sensei
unfortunately it still wont work

<?php
error_reporting(0);
include_once('../includes/config.php');
if(isset($_SESSION['roleID'])) 
{ 
    $result = mysql_query("SELECT roleID from blog_members where roleID='$_SESSION[roleID]'");
    $row = mysql_fetch_array($result); 
    if($row==1) 
    {
         echo "admin";
         }
  elseif ($row == 2 ) {
                    echo "faculty";
                }

}

?>
0

You didn't even connect to the database. I recommend you take the MySQLi or PDO example, NOT the deprecated MySQL one...

0

doesnt my config.php the one to handle connecting to database? here it is...:p sorry, i really am a novice..

<?php
ob_start();
session_start();
include('functions.php');

//database credentials
define('DBHOST','localhost');
define('DBUSER','root');
define('DBPASS','');
define('DBNAME','uslt');

$db = new PDO("mysql:host=localhost; dbname=".DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//set timezone
date_default_timezone_set('Europe/London');

//load classes as needed
function __autoload($class) {

   $class = strtolower($class);

    //if call from within assets adjust the path
   $classpath = 'classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }   

    //if call from within admin adjust the path
   $classpath = '../classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }

    //if call from within admin adjust the path
   $classpath = '../../classes/class.'.$class . '.php';
   if ( file_exists($classpath)) {
      require_once $classpath;
    }       

}

$user = new User($db); 

?>

Edited by RonKevinT.Manuela: wrong

1

doesnt my config.php the one to handle connecting to database?

Yes it does, but I couldn't see that.

The problem is that your config.php instantiates a PDO connection, so using the mysql_xxx() is not compatible with that. You need to be consistent.

0
<?php
error_reporting(0);
include_once('../includes/config.php');
// create a query that should return a single record
    // the backticks around the table and column names are optional
    // they are required only when a name matches a reserved word (e.g. `date`)
    $query = 'SELECT roleID FROM blog_members';
    // execute the query
    // query returns FALSE on error, and a result object on success
    try {
        $pdoStatement = $pdo->query($query);
    }
    catch (PDOException $exception) {
        // the query failed and debugging is enabled
        echo "<p>There was an error in query: $query</p>";
        echo $exception->getMessage();
        $pdoStatement = false;
    }
    if ($pdoStatement) {
        // the query was successful
        // get the result (if any)
        // fetchObject returns FALSE if there is no record
        if ($recordObj = $pdoStatement->fetchObject()) {
            // we have a record so now we can use it
            // the columns are properties of the object
            echo $recordObj->mycolumn;
        }
        else {
            echo '<p>No record found.</p>';
        }
        // when you are done with the statement, close it
        $pdoStatement->closeCursor();
    }

As I said Im a beginner and I dont fully understand it...I just replaced the query...

Edited by RonKevinT.Manuela

1

If you replace

echo $recordObj->mycolumn;

with

echo $recordObj->roleID;

do you see a value?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.