Jump to content

How do I assign an action based on the value of a row in my DB w/ sessions?


Jeff13246

Recommended Posts

Completely new to PHP here,

so on my website I'd like a specific image to appear according to whether or not the user in my DB has a row called 'premium', with a value of 1. I've tried figuring it out on my own but I'm stuck. I watched a video of someone doing something similar so this is what I have currently, but I'm aware it's totally incorrect.

//init.php file

<?php
if (session_status() == PHP_SESSION_NONE) {//php >5.4
  session_start();
}
$_SESSION['user_id'] = 1;   
$db = new PDO('mysql:host=127.0.0.1;dbname=loginsystem', 'root', '');
$userQuery = $db->prepare("
	SELECT idUsers, uidUsers, emailUsers, pwdUsers, premium
	FROM users 
	WHERE idUsers =:user_id
	");
	$userQuery->execute(['user_id' => $_SESSION['user_id']]);
	
	$user = $userQuery->fetchObject();

//webpage in which the image will appear
 

<?php
require_once '../includes/init.php';
?>
 <?php if($user-> premium): ?>
      <img src="../../img/premium.png" alt="Swiftify+"></a>
<?php else: ?>
      <img src="../../img/swiftify.png" alt="Swiftify+"></a>
<?php endif; ?>

 

Link to comment
Share on other sites

44 minutes ago, Barand said:

And what happens when you run it?

If I assign only one of the users as having a value of 1 for the premium row, the image:

"../../img/premium.png"

appears on each users profile.

I want this image to appear only on the users profile's who has the 'premium' row set as 1. 

I attached a picture of the user who has the specified row set as 1 if it helps to see. If I set it as 0, then the image

"../../img/swiftify.png"

appears on every users profile. 

premium.PNG

Also if it helps, when I var_dump($user) I get 

object(stdClass)#3 (5) { ["idUsers"]=> string(1) "1" ["uidUsers"]=> string(11) "beanbean123" ["emailUsers"]=> string(20) "jeffgee999@gmail.com" ["pwdUsers"]=> string(60) "$2y$10$VzYSkM7PoVTEzcY9rCnBXejCCMp0kAaXnSa630sBuXWkx/jqh6NSG" ["premium"]=> string(1) "1" }

 

Edited by Jeff13246
additional info
Link to comment
Share on other sites

29 minutes ago, Barand said:

The code you posted only fetches the first record. I don't see how it can behave as you describe.

Like I said I'm completely new to PHP haha. I've been learning the basics through instructional guides and I'm completely stuck on how I can make this work. If you can give a brief explanation of where to go next, it would be a lot of help!

Link to comment
Share on other sites

First off, I'd put just the connection code in the included file. You also need to add a few options when connecting with PDO. EG

    $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

Put the query and result processing in the main file.

$userQuery = $db->prepare("
    SELECT idUsers, uidUsers, emailUsers, pwdUsers, premium
    FROM users 
    WHERE idUsers =:user_id
    ");
$userQuery->execute(['user_id' => $_SESSION['user_id']]);
    
while ($user = $userQuery->fetchObject()) {
    echo  $user->premium ? '<img src="../../img/premium.png" alt="Swiftify+">' : '<img src="../../img/swiftify.png" alt="Swiftify+">' ;
    echo '<br>';
}

 

Link to comment
Share on other sites

1 hour ago, Jeff13246 said:

appears on each users profile.

probably because the code is assigning a 1 to the session variable.

your code should -

  1. unconditionally start the session, in a common 'required' configuration/initialization file.
  2. first detect if there is a logged in user. if there's not a logged in user, there's no point in querying to get any user data. you should instead set up a default set of $user data.
  3. if there is a logged in user, run the query to get the user's data. you should only SELECT the columns you want, and unless this is part of a login script, you should not SELECT the password hash.
  4. if the query didn't find any matching user data, you either have a programming mistake or a user's row got deleted between the time the user logged in and when you executed this query. if the query did find matching user data, fetch it to replace the default values set in item #2.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.