【问题标题】:View profile page PHP查看个人资料页面 PHP
【发布时间】:2014-06-26 14:38:20
【问题描述】:

所以我创建了一个管理面板,用于测试目的只是为了体验,我正在努力做到这一点,所以当您登录时(使用 PHP 会话),您可以单击按钮配置文件,它将带您进入您的配置文件并显示您的姓名、电子邮件、用户组。我的数据库中的所有这些,但我似乎无法显示它们..

这是我的代码

<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

sec_session_start();

if (isset($_GET['username'])) {
    $username = mysql_real_escape_string($_GET['username']);
    if (ctype_alnum($username)) {
        //check user exists
        $check = mysql_query("SELECT username, email, usergroup FROM members WHERE username='$username'");
        if (mysql_num_rows($check)===1) {
            $get = mysql_fetch_assoc($check);
            $username = $get['username'];
            $email = $get['email'];    
            $usergroup = $get['usergroup'];
        } else {
            echo "<h2>User does not exist!</h2>";    
            exit();
        }
    }
}
?>

<html>
    <head>
        <title>test</title>
    <body>
        <php? echo $username; ?>
    </body>
</html>

那么你有什么想法吗?

谢谢,

另外 - 我已经将 $username 定义为一个变量,但是当我显示它并加载页面时它只是说 Undefined variable: username in

【问题讨论】:

  • PHP 5.5.x 已弃用 MySQL,请使用 MysSQLi FunctionsMySQLi Class
  • var_dump() 你用 $_GET 看看你的 get 中是否有 'username'
  • 不太明白 ^ :} 有点新,抱歉。
  • 线索:&lt;php? echo $username; ?&gt;
  • @ilovecode 也在想同样的事情,但认为这只是问题中的一个错字......如果那是在真正的代码中,我认为他甚至不会得到undefined variable 错误

标签: php html css user-profile


【解决方案1】:

很难确定实际错误发生在哪里。那么让我们来看看吧。

让我们从您的包含开始:

include_once 'includes/db_connect.php';
include_once 'includes/functions.php';

这是两个未知来源。很难说有没有问题。

如果检查 URI 传递的用户名:

if (isset($_GET['username'])) {

这可能会导致错误(虽然它不太可能在您的情况下引发错误),因为如果您传递一个没有值的 GET 变量键(例如,index.php?foo 导致isset($_GET['foo']) == true,而!empty($_GET['foo']) == false。所以在这里使用!empty() 而不是isset() 很方便。

您检查用户名是否为字母数字:

if (ctype_alnum($username)) {

由于ctype_alnum() "checks if all of the characters in the provided string [...] are alphanumeric",错误可能是由于传递了包含非字母数字字符的用户名。

您没有用于字母数字检查的else {} 定义。考虑这样做,例如通过添加

if(ctype_alumn(...)) {
    // ...
} else {
    echo "<h2>Alphanumeric username required!</h2>";
    exit();
}

你的 MySQL fetch 似乎没问题,否则你的

echo "<h2>User does not exist!</h2>";   
exit();

会去拿的。

最后,还可以考虑为您的 isset($_GET['username']) 检查添加 else 情况,例如像这样

if (isset($_GET['username'])) {
    // ...
} else {
    echo "<h2>No username provided!</h2>";
    exit();
}

关于var_dump() 用法的旁注

@Andy 评论了使用var_dump() 转储有关$_GET['username'] 的信息的建议。这是一个例子:

if (isset($_GET['username'])) {
    var_dump($_GET['username']);
    exit();
}

会打印出类似的东西

string(7) "Foo Bar"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-08
    • 2016-11-26
    • 1970-01-01
    • 2012-09-30
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多