我有一个链接可以“切换”一个 div 以在名为“notifications.php”的页面中显示和隐藏。我想从名为“getnotes.php”的页面中检索 div 中的数据。
我成功的是让 div 显示和隐藏,以及让 div 回显 getnotes.php 文件的任何内容,但是当我尝试使用 SQL 在 getnotes.php 页面中获取数据时,它不会得到信息。
在下面的代码中,我只是试图让用户名显示。它将不仅仅用于检索用户名,但就本线程而言,我们正在检索用户名。
通知.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>
获取笔记.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";
为什么 SQL 查询在 getnotes.php 中不起作用?