0

I am having trouble making a PHP code. The PHP code a header navigation. In this header, It will show different headers for different users. I had added a column in my SQL database called "groups". I had added the default "0" as a INT. So when users make an account, It sets the "groups" to value 0.

In my admin panel, I have a table where you can edit the value on the groups. Users value is "0" and Admins value is "1"

The part I am stuck on is actually making the code to work. I tried to research on my issue but I could not find any solution.

Here is the code:

<?php
include('authCheck.php');
include('dbCredentials.php');
$group = isset($_GET['group']);

// Query
$sql = "SELECT * FROM AccountInfo WHERE group = :group";
$cmd = $conn->prepare($sql);
$cmd->bindParam(':group', $group, PDO::PARAM_INT);

// Run query
$cmd->execute();
$groups = $cmd->fetch(PDO::FETCH_ASSOC);

if ($groups['group'] < 0) {
    echo '<li><a href="#">Home</a> </li>
           <li><a href="#">Edit List</a> </li>
           <li><a href="#">Logout</a> </li>';
} else if ($groups['group'] < 1) {
    echo '<li><a href="#">Admin Panel</a> </li>
        <li><a href="#">Home</a> </li>
        <li><a href="#">Logoff</a> </li>';
} else {
    echo '<li><a href="login.php">Login</a> </li>
        <li><a href="register.php">SignUp</a> </li>';
}
?>

Please help, thank you! :)

EDIT

Here is the SQL DB

CREATE TABLE AccountInfo
(
    user_id INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
    email VARCHAR(50),
    password CHAR(128),
    group INT(2) DEFAULT '0'
);

The USER_ID is used to identify each user by ID.

1 Answers1

0

Easy peasy...change to:

if ($groups['group'] == 0) {
    echo '<li><a href="#">Home</a> </li>
        <li><a href="#">Edit List</a> </li>
        <li><a href="#">Logout</a> </li>';
} else if ($groups['group'] == 1) {
    echo '<li><a href="#">Admin Panel</a> </li>
        <li><a href="#">Home</a> </li>
        <li><a href="#">Logoff</a> </li>';
} else {
    echo '<li><a href="login.php">Login</a> </li>
        <li><a href="register.php">SignUp</a> </li>';
}
adomas.m
  • 383
  • 2
  • 12
  • That's exactly what I did but It wouldn't work for some odd reason – Frank Amaral Apr 01 '16 at 12:53
  • Do you get any specific error? Which items are shown on menu: Admin? Login? Usual users group? – adomas.m Apr 01 '16 at 12:55
  • Maybe my column is set wrong? I made it a INT not null and when a new account is made, it defaults to 0 – Frank Amaral Apr 01 '16 at 13:02
  • No I don't get an error but when I go to the page, It shows the navigation for regular users when i sign in. Maybe I need to indicate the user_id? – Frank Amaral Apr 01 '16 at 13:03
  • My primary key is user_id – Frank Amaral Apr 01 '16 at 13:03
  • Hm...but why do you run the query at all? You get the group id over $_GET and then run the query to get group id? Because in SQL you limit results by column group and then take column group from results as well. It seems that query is not necessary at all. Unless you get user_id over $_GET and then have to determine user of group. Can you drop description of sql table and some data example? – adomas.m Apr 01 '16 at 13:40
  • i added the sql above :) – Frank Amaral Apr 01 '16 at 13:56
  • the user_id is an id number for each registered user. this website is a cms. In the table, I added "group" for group rights. regular user = default 0, admin = 1. – Frank Amaral Apr 01 '16 at 14:11