David
David

Reputation: 184

Trying to get property of a non object

I've came across this problem before, but I fixed and it was actually simple. I can't remember what I did to fix it.. :/ It's not showing me as premium at the bottom of my page, my purchase goes through, it just doesn't update because it says, 'trying to get property of a non object' at the top of my page. EDIT: It also doesn't update in the database, but when I update manually it still doesn't show I'm premium on the webpage.

Here is where the problem is, index.php

    <?php if($users->premium): ?>
    <p id="prem" style="position: fixed; bottom: 0; width:100%; text-align: center">You are <span style="color:#FB0307">PREMIUM</span>!</p>
    <?php else: ?>
    <p id="prem" style="position: fixed; bottom: 0; width:100%; text-align: center">You are not premium. <a href="checkout.php"><span style="color:#FB0307">Go premium!</span></a></p>
    <?php endif; ?>

To fix this problem, this is the code I'm pretty sure I had to edit the last time. 'It was actually something really small' lol

    $_SESSION['user_id'] = '';

$stripe = array(
    "secret_key" => "sk_test_hzpYzgCFLBf3sTFtqzYX8Qqy",
    "publishable_key" => "pk_test_AdvDAYfsO9vdYBXJiPaucNHw"
    ); // Test keys

    \Stripe\Stripe::setApiKey($stripe['secret_key']);

    $db = new PDO('mysql:host=127.0.0.1;dbname=blog', 'root', '');

$userQuery = $db->prepare("SELECT id, username, email, premium FROM users WHERE username = :username");

    $userQuery->execute(array(':username' => $_SESSION['user_id']));

    $users = $userQuery->fetchObject();

I highly doubt this has anything to do with it, but this creates the $_SESSION at the top of the page for the 2nd code block. There is more than this if($login) statement, but this is the only statement pertaining to this.

if($login){
            $_SESSION["user_id"] = Input::get('username');
             Redirect::to('index.php');
            }else{
                 echo '<p style="color: white;">Sorry, login failed.</p>';
        }

Upvotes: 3

Views: 1595

Answers (1)

Darren
Darren

Reputation: 13128

Your issue is due to the fact that your $users variable is not an object. Meaning that your query failed.

Which would lead me to believe it's because of the $_SESSION['user_id'].

We can't tell from your code supplied, but you always need to start the session on each and every page you want to use sessions on:

session_start();

Also, you set the user session id to empty before you run your query...

$_SESSION['user_id'] = '';

Which would warrant your query useless because now the user_id is empty.

Upvotes: 1

Related Questions