简体   繁体   English

在表中存储用户的名字和姓氏

[英]Storing the first name and last name of the user in a table

I've created a register and login for my website, and I've created a table to store the users first name and last name, but I want to store more than one user in the table, but every time I update the table, it replaces the first user every time a new user logs in. 我已经为网站创建了一个注册和登录名,并且创建了一个表来存储用户的名字和姓氏,但是我想在该表中存储多个用户,但是每次更新该表时,每当有新用户登录时,它都会替换第一个用户。

Click the link to view the images: 单击链接查看图像:

https://imgur.com/a/CFMC8 https://imgur.com/a/CFMC8

/*-- REGISTER --*/

function storeUserDetail(){
    var fNameInput = document.getElementById("firstNameInput").value;
    var lNameInput = document.getElementById("lastNameInput").value;
    var uNameInput = document.getElementById("userNameInput").value;
    var pWordInput = document.getElementById("passWordInput").value;
    if(fNameInput === ""){
        document.getElementById("regMessage").innerHTML = "<span 
        class='error'>Please enter your First Name.</span>";
    }
    else if(lNameInput === ""){
        document.getElementById("regMessage").innerHTML = "<span 
        class='error'>Please enter your Last Name.</span>";   
    }
    else if(uNameInput === ""){
        document.getElementById("regMessage").innerHTML = "<span 
        class='error'>Please enter your Username.</span>";   
    }
    else if(pWordInput === ""){
        document.getElementById("regMessage").innerHTML = "<span 
        class='error'>Please enter your Password.</span>";   
    }
    else {
        var storeDetails = {};
        storeDetails.FirstName = 
        document.getElementById("firstNameInput").value;
        storeDetails.LastName = 
        document.getElementById("lastNameInput").value;
        storeDetails.Username = 
        document.getElementById("userNameInput").value;
        storeDetails.Password = 
        document.getElementById("passWordInput").value;
        localStorage[storeDetails.Username] = JSON.stringify(storeDetails);
        window.location.replace("http://localhost/login.php");
    }                    
}

/*-- LOGIN -- */

function loginUser(){
    var Username = document.getElementById("userNameInput").value;
    var Password = document.getElementById("passWordInput").value;
    if(Username === ""){
        document.getElementById("logMessage").innerHTML = "<span 
        class='error'>Please enter your Username.</span>";                   
    }
    else if(Password === ""){
        document.getElementById("logMessage").innerHTML = "<span 
        class='error'>Please enter your Password.</span>";     
    }
    else {
        if(localStorage[Username] === undefined) {
        document.getElementById("logMessage").innerHTML = "<span 
        class='error'>Username Incorrect. Please try again.</span>";
        return;
    }
    else {
        var storeDetails = JSON.parse(localStorage[Username]);
        if(Password === storeDetails.password) {
            localStorage.loggedInUserName = storeDetails.Username;
            window.location.replace("http://localhost/game.php");
        }
        else{
            document.getElementById("logMessage").innerHTML = "<span 
            class='error'>Password Incorrect. Please try again.</span>";
        }
    }
}

/* TABLE */

function inputUserInfo(){
    var storeDetails = JSON.parse(localStorage[localStorage.LoggedInUser]);
    var table = document.getElementById("rankTable");
    var row = table.insertRow();
    var firstNameCell = row.insertCell(0);
    var lastNameCell = row.insertCell(1);
    firstNameCell.innerHTML = storeDetails.FirstName;
    lastNameCell.innerHTML = storeDetails.LastName;
}

You can't create a registration and login system using local storage. 您无法使用本地存储创建注册和登录系统。 Local storage only saves values in the user's own browser , thus 'local.' 本地存储仅将值保存在用户自己的浏览器中 ,因此保存为“本地”。 The server doesn't know about them, other users don't know about them, and they're all cleared a way if the user clears their browser history thoroughly. 服务器不了解它们,其他用户也不了解它们,并且如果用户彻底清除其浏览器历史记录,则可以全部清除。 And there is only one value for each given key -- if you say username = "Sarah" today, and username = "Laura" tomorrow, then Laura overwrites Sarah, because it just doesn't make sense for the user's browser to have two different things both called username . 而且每个给定键只有一个值-如果您今天说username = "Sarah" ,明天说username = "Sarah" username = "Laura" ,那么Laura会覆盖Sarah,因为对于用户浏览器来说,拥有两个是没有意义的不同的东西都称为username

Local storage isn't suitable for registration and login systems. 本地存储不适用于注册和登录系统。 It's only suitable for caching things for an individual user. 它仅适用于为单个用户缓存内容。 For example, you might store in-progress/unsent messages in local storage so the user doesn't lose them on a page refresh. 例如,您可以将进行中/未发送的消息存储在本地存储中,以使用户在刷新页面时不会丢失它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM