Trying to get a property of a non-object
I ran into this problem before, but I fixed it and it was simple. I can't remember what I did to fix this.: / It doesn't show me the premium at the bottom of my page, my purchase goes through, it just doesn't update because it says "trying to get a non object property" at the top my page. EDIT: It also doesn't update in the database, but when I manually update it still doesn't show that I'm a premium on the webpage.
That's 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 issue, this is the code I'm sure I had to edit last time. "It was really something very 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 very much doubt it has anything to do with this, but it creates $_SESSION
a top of the page for the second block of code. There is more than this operator if($login)
, but this is the only statement related to it.
if($login){
$_SESSION["user_id"] = Input::get('username');
Redirect::to('index.php');
}else{
echo '<p style="color: white;">Sorry, login failed.</p>';
}
source to share
Your problem is because your variable is $users
not an object. This means that your request has failed.
Which would make me believe it because of $_SESSION['user_id']
.
We can't tell from your code, but you always need to start a session on every page you want to use sessions on:
session_start();
Also, you set the user's session id to empty before running your request ...
$_SESSION['user_id'] = '';
Which would guarantee your request is useless because it is now user_id
empty.
source to share