如何使用 HTML、PHP 和 MySQL 创建登录表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22147935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:41:34  来源:igfitidea点击:

How to create a login form with HTML, PHP and MySQL

phphtmlmysql

提问by littleswany

UPDATE [13.10.16]

更新 [13.10.16]

So coming back to one of my previous questions on stackoverflow that has low reputation and for reasons I can see why, I wanted to update it and make it much more relevant.

因此,回到我之前关于 stackoverflow 的一个声誉不佳的问题,出于我能理解的原因,我想更新它并使其更具相关性。

When I wrote this question I wanted to create a login system from a basic html form, it is a much more complicate process.

当我写这个问题时,我想从一个基本的 html 表单创建一个登录系统,这是一个复杂得多的过程。

For anyone seeing this question I would recommend that you use a PHP framework that does all the handling for you. I would suggest using Laravel as it has all the framework you need for a simple login system and much more without the need of lots of PHP which would be needed for the example below.

对于任何看到这个问题的人,我建议您使用一个 PHP 框架来为您完成所有处理。我建议使用 Laravel,因为它具有简单登录系统所需的所有框架,而且不需要大量 PHP,而下面的示例则需要这些框架。



Original Question

原始问题

How do I create a login form with HTMl, PHP, and MySQL?

如何使用 HTMl、PHP 和 MySQL 创建登录表单?

I created a form as seen below:

我创建了一个表单,如下所示:

<form id="login">


    <h2 id="logintxt">Log In</h2>
    <h1 id="username">User Name:</h1>
    <input name="Username" type="text" value="" maxlength="20" id="usernamebox" />
    <h1 id="password">Password:</h1>
    <input name="Password" type="password" id="passwordtxt" />
    <h1 id="forgot">Forgot Password</h1>
    <h1 id="register">Register</h1>
    <div id="submit">
    <img src="login.gif" width="100" height="40" />
    </form>

I have seen tutorials when having a form navigate to another page to login however I do not like this is there any way around it?

当让表单导航到另一个页面进行登录时,我已经看过教程,但是我不喜欢这样,有什么办法可以解决吗?

My MySQl data is:

我的 MySQl 数据是:

Host: localhost
Username: root
Password: root
Database: the_tech

Thanks

谢谢

[UPDATE]

[更新]

Here is my full html page. http://pastebin.com/NvgzT0Xu

这是我的完整 html 页面。http://pastebin.com/NvgzT0Xu

From this what is required to create a html page login? Preferably without leaving the page.

从这需要什么来创建一个 html 页面登录?最好不要离开页面。

I tried adding this (http://pastebin.com/1HcZGsUS) to the top of my page however it did not work. I was following this (https://www.scirra.com/tutorials/525/simple-login-using-a-mysql-database) tutorial.

我尝试将此(http://pastebin.com/1HcZGsUS)添加到我的页面顶部,但它不起作用。我正在关注这个(https://www.scirra.com/tutorials/525/simple-login-using-a-mysql-database)教程。

Thanks

谢谢

回答by Himanshu Jaju

You need a backend php file to connect with the database and query it. TO avoid moving to another page (Front end), you can use ajax and connect to the php file via javascript.

您需要一个后端 php 文件来连接数据库并进行查询。为了避免移动到另一个页面(前端),您可以使用 ajax 并通过 javascript 连接到 php 文件。

You can follow this tutorial : http://php-dev-zone.blogspot.in/2013/07/login-form-using-ajax-and-jquery.html

您可以按照本教程进行操作:http: //php-dev-zone.blogspot.in/2013/07/login-form-using-ajax-and-jquery.html

回答by sagschni

First you need a form with this parameters arround the input-fields and a button.

首先,您需要一个带有此参数的表单,围绕输入字段和一个按钮。

<form method="post" action="checklogin.php">
       <h2 id="logintxt">Log In</h2>
       <h1 id="username">User Name:</h1>
       <input name="Username" type="text" value="" maxlength="20" id="usernamebox" />
       <h1 id="password">Password:</h1>
       <input name="Password" type="password" id="passwordtxt" />
       <h1 id="forgot">Forgot Password</h1>
       <h1 id="register">Register</h1>
       <img src="login.gif" width="100" height="40" />

       <input type="submit" value="Login">
</form>

And then you need Php to check if the username and password was correct. When you don't like to go to an other page after login, what you want instead? Should the index page check it by itself?

然后你需要Php来检查用户名和密码是否正确。当您登录后不喜欢转到其他页面时,您想要什么?索引页应该自己检查吗?

Then you could check if the $_POST of the input fields isset. If yes, connect to the database an look if username and password is right. If no $_POST isset, Show the Loginform. I think there are many examples looking to Google.

然后你可以检查输入字段的 $_POST 是否设置。如果是,连接到数据库查看用户名和密码是否正确。如果没有 $_POST 设置,则显示登录表单。我认为有很多例子都在寻找谷歌。