【问题标题】:Undefined variable: username in C:\xampp\htdocs\test.php on line 26 [closed]未定义的变量:第 26 行 C:\xampp\htdocs\test.php 中的用户名 [关闭]
【发布时间】:2016-06-04 06:16:24
【问题描述】:

我想使用 GET 方法通过我的地址栏检查用户是否存在。如果该用户存在,则回显 $username 和 $firstname。但它显示 2 个错误。不知道怎么回事!

 <?php include ("./trial/header.php"); ?>
<?php
    //we want to perform  http://localhost/test.php?=jude to see if user exists then displays username and first name
    //the table name is users and include username, first_name, last_name, password and email.
if (isset($_GET['u'])) {
    $username = mysqli_real_escape_string($con, $_GET['u']);
    if (ctype_alnum($username)) {
    //check if user exists
    $check = mysqli_query($con, "SELECT username, first_name FROM users WHERE username='$username'");
    //if user exists we want to display username and firstname on the page
    if (mysqli_num_rows($check)===1) {
    $get = mysqli_fetch_assoc($check);
    $username = $get['username'];
    $firstname = $get['firstname']; 
    }
    //if user do not exist we want to to display "The user does not exist"
    else
    {
    echo "The user does not exist";  //no existing users
    exit();
    }
    }
}
?>

    <h2>Profile page for: <?php echo $username;?></h2>;
    <h2>First name: <?php echo $firstname;?></h2>;

【问题讨论】:

  • firstname 不等于 first_name
  • http://localhost/test.php?=jude 将是http://localhost/test.php?u=jude
  • 把`$username=""; $名 = ""; `就在if (isset($_GET['u'])) {之前,看看错误是否消失了?我认为您的代码没有输入您的if statement...
  • @PraveenKumar 这就是答案,你应该发布它。

标签: php html sql mysqli


【解决方案1】:

您的if statement 有问题您的代码没有输入... 作为一种解决方法,我可以给你代码......

<?php include ("./trial/header.php"); ?>
<?php
    //we want to perform  http://localhost/test.php?=jude to see if user exists then displays username and first name
    //the table name is users and include username, first_name, last_name, password and email.
$username = ""; //Added this 
$firstname = "";//Added this
if (isset($_GET['u'])) 
{
    $username = mysqli_real_escape_string($con, $_GET['u']);
    if (ctype_alnum($username)) 
    {
        //check if user exists
        $check = mysqli_query($con, "SELECT username, first_name FROM users WHERE username='$username'");
        //if user exists we want to display username and firstname on the page
        if (mysqli_num_rows($check)===1) 
        {
            $get = mysqli_fetch_assoc($check);
            $username = $get['username'];
            $firstname = $get['first_name']; 
        }
        //if user do not exist we want to to display "The user does not exist"
        else
        {
            echo "The user does not exist";  //no existing users
            exit();
        }
    }
}
?>

<h2>Profile page for: <?php echo $username;?></h2>;
<h2>First name: <?php echo $firstname;?></h2>;

要获得正确的输出,您应该使用此 URL http://localhost/test.php?u=jude 代替 http://localhost/test.php?=jude As Md. Sahadat Hossain 建议。

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多