我的登录表格有用户名和密码字段。当用户使用用户名和密码登录时,我想显示用户的全名而不是他们的用户名。在下一页上,我想说欢迎FULLNAME,而不是欢迎USERNAME。
这是index.php或登录页面的html代码:
<form action="" method="POST"> <div class="rows grid"> <div class="row"> <label for="username">用户名</label> <input type="text" id="username" name="userName" placeholder="输入用户名" required> </div> <!--密码--> <div class="row"> <label for="password">密码</label> <input type="password" id="password" name="passWord" placeholder="输入密码" required> </div> <!--提交按钮--> <div class="row"> <input type="submit" id="submitBtn" name="submit" value="登录" required> <!--注册链接--> <span class="registerLink">还没有账号?<a href="register.php">注册</a></span> </div> </div> </form> </div> </div>
这是php代码:
<?php // 提交 if(isset($_POST['submit'])){ // 存储用户名、密码、邮箱、号码 $userName = $_POST['userName']; $passWord = $_POST['passWord']; // 从数据库中选择 $sql = "SELECT * FROM admin WHERE usernames = '$userName' AND passwords = '$passWord'"; // 执行查询 $result = mysqli_query($conn, $sql); // 计算相同用户名和密码的帐户数量 $count = mysqli_num_rows($result); // 将结果计数到数组中 $row = mysqli_fetch_assoc($result); // 检查数据库中是否有帐户 if ($count == 1){ $_SESSION['loginMessage'] = '<span class = "success">欢迎 '.$userName.'</span>'; header('location:' .SITEURL. 'dashboard.php'); exit(); } else{ $_SESSION['noAdmin'] = '<span class = "fail">请检查您的用户名和密码,然后重试。</span>'; header('location:' .SITEURL. 'index.php'); exit(); } } ?>
这是register.php或注册链接:
<form action="" method="POST"> <div class="rows grid"> <!--全名--> <div class="row"> <label for="fullname">全名</label> <input type="text" id="fullname" name="fullName" placeholder="输入全名" required> </div> <!--用户名--> <div class="row"> <label for="username">用户名</label> <input type="text" id="username" name="userName" placeholder="输入用户名" required> </div> <!--邮箱--> <div class="row"> <label for="email">邮箱</label> <input type="email" id="email" name="emaiL" placeholder="输入邮箱" required> </div> <!--手机号码--> <div class="row"> <label for="number">手机号码</label> <input type="number" id="number" name="numbeR" placeholder="输入手机号码" required> </div> <!--密码--> <div class="row"> <label for="password">密码</label> <input type="password" id="password" name="passWord" placeholder="输入密码" required> </div> <!--提交按钮--> <div class="row"> <input type="submit" id="submitBtn" name="submit" value="注册" required> <!--已有账号?--> <span class="registerLink">已经有账号了?<a href="index.php">登录</a></span> </div> </div> </form>
注意:我想在欢迎仪表板中显示输入的全名。
P粉4015270452024-04-02 17:21:47
$row
,然后使用 $row["fullname"]
(如果字段名实际上是 fullname
,则使用 $row["fullName"]
)因此,将以下代码块更改为:
/// 其他代码 $sql = "SELECT * FROM admin WHERE usernames = '$userName' AND passwords = '$passWord'"; // 执行查询 $result = mysqli_query($conn, $sql); // 计算相同用户名和密码的帐户数 $count = mysqli_num_rows($result); // 将结果计算到数组中 $row = mysqli_fetch_assoc($result); // 检查数据库中是否有帐户 if ($count == 1){ $_SESSION['loginMessage'] = '<span class = "success">欢迎 '.$userName.'</span>'; header('location:' .SITEURL. 'dashboard.php'); exit(); } /// 其他代码
更改为:
<?php /// 其他代码 $sql = "SELECT * FROM admin WHERE usernames = ? AND passwords = ?"; $query = $conn->prepare($sql); $query->bind_param("ss", $userName,$passWord ); $query->execute(); $result = $query->get_result(); $num = $result->num_rows; if ($num == 1){ $row = $result->fetch_assoc(); $_SESSION['loginMessage'] = '<span class = "success">欢迎 '.$row["fullname"].'</span>'; header('location:' .SITEURL. 'dashboard.php'); exit(); } /// 其他代码 ?>