Reputation: 305
I've got a link that "toggles" a div to show and hide in a page called "notifications.php". I want to data in the div to be retrieved from a page called "getnotes.php".
I've succeeded is get the div to show and hide, as well as getting the div to echo whatever the getnotes.php file has but when I try to use SQL to get data in the getnotes.php page it won't get the information.
In the code below I'm simply trying to get the username to display. It will be used for more than retrieving a username but for the purposes of this thread, we're retrieving a username.
notifications.php
//allow sessions to be passed so we can see if the user is logged in
session_start();
ob_start();
//connect to the database so we can check, edit, or insert data to our users table
$con = mysql_connect('SERVER', 'NAME', 'PASSWORD') or die(mysql_error());
$db = mysql_select_db('NAME', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "./functions.php";
//In the header
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
function toggleDiv(divId) {
$("#"+divId).toggle();
$(document).ready(function(){
$("#myContent").load("getnotes.php?name=<? echo $username; ?>");
});
}
</script>
//In the body
<a href="javascript:toggleDiv('myContent');">Notes</a><br>
<div id="myContent" style="display:none; background-color: #aaa; padding: 5px 10px;">
</div>
getnotes.php
//allow sessions to be passed so we can see if the user is logged in
session_start();
ob_start();
//connect to the database so we can check, edit, or insert data to our users table
$con = mysql_connect('SERVER', 'NAME', 'PASSWORD') or die(mysql_error());
$db = mysql_select_db('NAME', $con) or die(mysql_error());
//include out functions file giving us access to the protect() function made earlier
include "./functions.php";
//Look up the username and see if they are logged in.
$userid = $_SESSION['uid'];
$lookupusername = mysql_query("SELECT * FROM users WHERE ID='$userid'");
$row = mysql_fetch_assoc($lookupusername);
$username = $row['username'];
echo "you are $username";
Why won't the SQL queries work in getnotes.php?
Upvotes: 0
Views: 378
Reputation: 976
Your code looks ok, you probably have a problem somewhere else. Try to print your session:
print_r($_SESSION);
and look if $_SESSION['uid']
has the right user id.
Upvotes: 0
Reputation: 4881
$lookupusername = mysql_query("SELECT * FROM users WHERE ID=" + $userid);
But please, use prepared statements.
Upvotes: 1