我有一个MySQL数据库,当前有一个名为“ loginInfo”的表,该表存储用户的电子邮件,电话号码,用户名和密码。他们登录后,我希望他们可以选择更新他们的电子邮件和/或电话号码(密码是单独处理的,但我认为会与此类似。)我知道我必须使用“更新loginInfo SET电子邮件= newemail@test.com,其中用户名=登录用户名”;我的问题是,我不知道如何获取当前登录用户的用户名是。我有一个login.php脚本,该脚本在表中查找匹配的用户名和密码,以及createAccount.php脚本,该脚本也向该表中添加了电子邮件/用户名/密码。
我相信只要知道如何获取当前登录的用户名,我就会知道该怎么做,只是不了解我该怎么做。
login.php
<?php
session_start();
include_once 'conn.php';
if(empty($_POST['username']) || empty($_POST['password']))
header('Location: ../html/index.html');
else {
if(isset($_POST['loginBtn'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$password = md5($password);
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
$query = "SELECT username, password FROM loginInfo WHERE username=? AND password=? LIMIT 1";
$stmt = $conn->prepare($query);
$stmt -> bind_param("ss", $username, $password);
$stmt -> execute();
$stmt -> bind_result($username, $password);
$stmt -> store_result();
if($stmt->fetch()) {
header('Location: ../html/mainPage.html');
}
else {
echo "Error logging in";
}
mysqli_close($conn);
}
}
?>
updateInfo.html
<html>
<link rel="stylesheet" href="../css/updateInfo.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<body>
<div class="header">
<h1 id="head1">Lost And Found</h1>
<div class="navbar">
<a href="mainPage.html" class="home-btn">Home</a>
<div class="profile-dropdown">
<button class="profile-btn">Profile <i class="fa fa-caret-down"></i></button>
<div class="profile-content">
<a href="yourListings.html">Your Listings</a>
<a href="updateInfo.html">Update Info</a>
</div>
</div>
<div class="search-dropdown">
<button class="search-btn">Search <i class="fa fa-caret-down"></i></button>
<div class="search-content">
<a href="searchLost.html">Search Lost</a>
<a href="searchFound.html">Search Found</a>
</div>
</div>
<div class="listings-dropdown">
<button class="listings-btn">Listings <i class="fa fa-caret-down"></i></button>
<div class="listings-content">
<a href="reportLost.html">Report Lost</a>
<a href="reportFound.html">Report Found</a>
</div>
</div>
<div class="settings-dropdown">
<button class="settings-btn">Settings <i class="fa fa-caret-down"></i></button>
<div class="settings-content">
<a href="changePassword.html">Change Password</a>
</div>
</div>
<a href="index.html" class="logout-btn">Logout</a>
</div>
</div>
<script type="text/javascript">
var nav = document.getElementsByClassName("navbar");
window.onscroll = function sticky() {
if(window.pageYOffset > nav[0].offsetTop) {
nav[0].classList.add("nav");
}
else {
nav[0].classList.remove("nav");
}
}
</script>
<form class="login-box" action="../php/updateInfo.php" method="post">
<h1 id="header3">Update Account Info</h1>
<input type="text" name="newEmail" placeholder="New Email">
<input type="text" name="newPhone" placeholder="New/Add Phone Number">
<input type="submit" name="updateBtn" value="Update">
<a href="createAccount.html">Want to change your password?</a>
</form>
<div class="footer">
<a class="social-btn" href="http://www.facebook.com"><i class="fa fa-facebook"></i></a>
<a class="social-btn" href="http://www.twitter.com"><i class="fa fa-twitter"></i></a>
<a class="social-btn" href="http://www.youtube.com"><i class="fa fa-youtube"></i></a>
<a class="social-btn" href="http://www.instagram.com"><i class="fa fa-instagram"></i></a>
</div>
</body>
</html>
预期结果将使当前登录的用户在loginInfo表中更新其电子邮件/电话号码,替换其旧信息。
答案 0 :(得分:0)
因此,如果我没有看错您的文字,那么您需要知道如何在其他位置获取登录的用户名。
这是您可以做到的方式。
成功登录后,将用户名保存在session
中。
if($ stmt-> fetch()){ $ _SESSION ['username'] = $ username; header('Location:../html/mainPage.html'); }
然后在其他任何地方,您都可以使用username
阅读登录的$_SESSION['username']
。
请注意,在使用$ _SESSION的页面上,必须像在session_start();
文件中所做的那样在第一行添加login.php
。
而且,请记住在用户退出时取消设置$_SESSION['username']
。