Display link after user logs in based on sql.

I need help with a few lines of php. I have no clue on how to write it. But I would like it to go something like this.

After a user logs in, check the database (lets say row d). If it equals ‘1’ then echo a url. Otherwise do not echo a url.

I want to be able to go into my database and just put 1 in the same row of a user to allow them to see a link once they login. Some members have access to this, some don’t.

check the value of row d and display the link (or don’t do anything if not allowed)
[php]
if($row[‘d’] == 1) { echo ‘link’; } // check row d if allowed to view link
[/php]

Red.

:wink:

Thanks!

-Although the link doesn’t show up. Do I have to specify which database to look in? the login php page has a few lines about connecting to the database. I assumed it would be looking in the same one, but that code isn’t on this page. Should I just copy/paste that above your code so it knows which database to find row ‘d’ in?

Sorry I am kind of figuring this out as I go.
Looks like I will need the;

mysql_connect(“localhost”,“login”,“password”);
mysql_select_db(“db_name”);

But I will also need your code to say which field before i tell it which row.
so something like:

if ($fieldnamehere, $row['d] == 1) { echo etc…
?
Thanks again.

Hi mate, i used row d because you did…

your nearly right in your last post, the connection is fine and the database select is good too, however you didn’t add the query for me to read so i can’t be sure what the row would be called but basically goes like this:

[php]$query = “SELECT username, password, elevatedStatus FROM myDatabase WHERE user=’$user’ AND pass=’$password’ LIMIT 1”; // Fetch 1 row only.
$result = mysql_query($query); // run the query
$row = mysql_fetch_array($result, MYSQL_ASSOC); // set the info into an array called $row

if($row[‘elevatedStatus’] == 1) { echo ‘link’; } // check $row[‘elevatedStatus’] if allowed to view link

// $row[‘username’] holds the username
// $row[‘password’] holds the password
// $row[‘elevatedStatus’] holds the value of ‘elevatedStatus’ (either 1 or 0)
[/php]

remember, the data/values i used here (username, password, elevatedStatus) may not be what you actually have in your database, without seeing you query i don’t know. so change them accordingly.

Hope this helps,

Red. :wink:

I am receiving “mysql_fetch_array(): supplied argument is not a valid MySQL result resource” error on this line
[php]$row = mysql_fetch_array($result, MYSQL_ASSOC); // set the info into an array called $row[/php]

Which I think just means that I didn’t rename one of your examples properly.

database name is ‘noaamemb_PrimaryMembers’. The table I want to get info from is called ‘users’ and the fields with in are ‘username’ contains usernames.
‘password’ contains passwords and
‘bstorm’ which is my original ‘d’ or what I think you named ‘elevatedStatus’

It’s confusing because the php code contains username, user, $user and I am not sure which one is which and who’s on first.

[php]<?php
mysql_connect(“localhost”,“login”,“password”); // remember to put your actual details here!
mysql_select_db(“noaamemb_PrimaryMembers”);

$query = “SELECT username, password, bstorm FROM users WHERE username=‘john’ AND password=‘abc123’ LIMIT 1”; // Fetch 1 row only.
$result = mysql_query($query); // run the query
$row = mysql_fetch_array($result, MYSQL_ASSOC); // set the info into an array called $row

if($row[‘bstorm’] == 1) { echo ‘link’; } // check $row[‘bstorm’] if allowed to view link
?>
[/php]

try this… :wink: (notice i’ve changed username (john) and password (abc123) in the query to avoid any confusion between text, columns and variables.)

and your right, who is on first base :smiley:

I think I am going about this wrong. Unless there is a way to reference a previous entry in the login.php page.

I need
[php]username=‘john’ [/php]
to be
username=‘username entered from login.php’. The page I am working on now is memberzone.php, by the way.

here is the code from the login.php page.

[php] <?php
if(!empty($_SESSION[‘LoggedIn’]) && !empty($_SESSION[‘Username’]))
{
?>

Member Area

Thanks for logging in!

To search again, please click HERE

<ul>
    <li><a href="logout.php">Logout.</a></li>
</ul>

<?php

}
elseif(!empty($_POST[‘username’]) && !empty($_POST[‘password’]))
{
$username = mysql_real_escape_string($_POST[‘username’]);
$password = md5(mysql_real_escape_string($_POST[‘password’]));

 $checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");

if(mysql_num_rows($checklogin) == 1)
{
	 $row = mysql_fetch_array($checklogin);
    $email = $row['EmailAddress'];
    
    $_SESSION['Username'] = $username;
    $_SESSION['EmailAddress'] = $email;
    $_SESSION['LoggedIn'] = 1;
    
	 echo "<h1>Welcome</h1>";
    echo "<p>We are now redirecting you to the member area.</p>";
    echo "<meta http-equiv='refresh' content='0;url=memberzone-test.php'>";
}
else
{
	 echo "<h1>Error</h1>";
    echo "<p>Sorry, your account could not be found. Please <a href=\"login-test.php\">click here to try again</a>.</p>";
}

}
else
{
?>

Member Login

Thanks for visiting! Please either login below, or click here to register.

<form method="post" action="login.php" name="loginform" id="loginform">
<fieldset>
	<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
	<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
	<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
<?php } ?>

[/php]

I might just sit down and do a bunch of tutorials to understand php and mysql better. I have figured out how to update the database user info… Obviously I didn’t put this site together. I am just responsible for updating it ;D. Thanks again for the help.

this is the value in the form:
<input type=“text” name=“username” id=“username” />

it will be accessed by [php]$_POST[‘username’];[/php]

i did notice something though that i need to clear up because it may be causing a problem.
in one of your previous posts you stated:
[tt]
‘username’ contains usernames.
‘password’ contains passwords
[/tt]
notice you use all lowercase.

then here you refer to the username/password columns using a capitol first letter,
[php]$checklogin = mysql_query(“SELECT * FROM users WHERE Username = '”.$username."’ AND Password = ‘".$password."’");[/php]
so you have (username/Username) (password/Password), which are not the same!
maybe that was accidental in this post, but i need to check.

Sponsor our Newsletter | Privacy Policy | Terms of Service