未定义变量:第26行C:\xampp\htdocs\test.php中的用户名
我想使用GET方法通过我的地址栏检查用户是否存在。如果该用户存在,则回显$username和$firstname。但它显示了两个错误。我不知道怎么了 如果您的代码没有输入if语句,则该语句有问题。。。 作为一种解决方法,我可以为您提供代码未定义变量:第26行C:\xampp\htdocs\test.php中的用户名,php,html,sql,mysqli,Php,Html,Sql,Mysqli,我想使用GET方法通过我的地址栏检查用户是否存在。如果该用户存在,则回显$username和$firstname。但它显示了两个错误。我不知道怎么了 如果您的代码没有输入if语句,则该语句有问题。。。 作为一种解决方法,我可以为您提供代码 <?php include ("./trial/header.php"); ?> <?php //we want to perform http://localhost/test.php?=jude to see if user e
<?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>;
要获得正确的输出,您应该使用此urlhttp://localhost/test.php?u=jude 代替http://localhost/test.php?=jude 正如Sahadat Hossain博士所建议的那样。firstname不等于first_namehttp://localhost/test.php?=jude 会的http://localhost/test.php?u=judePut `$username=$名字=;`就在if-isset$_GET['u']{并查看错误是否消失之前?我认为您的代码没有输入if语句…@PraveenKumar这就是答案,您应该发布它。可能是
<?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>;