【发布时间】:2018-11-30 02:27:52
【问题描述】:
我有一个 html 登录表单。它收集了用户的用户名和密码。我还有一个收集它们并存储它的脚本。我想知道是否可以在存储到数据库之前检查用户名和密码是否已经存在于 MySQL 数据库中。
HTML 格式:
<!DOCTYPE html>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="login.py" method="GET">
First name:<br>
<input type="text" name="user_name"><br>
Last name:<br>
<input type="text" name="password"><br>
</form>
</body>
</html>
Login script
import cgitb
cgitb.enable()
import cgi
form = cgi.FieldStorage()
user_name = form["user_name"].value
password = form["password"].value
import pymysql
conn = pymysql.connect(db='userdb',
user='root', passwd='12346',
host='localhost')
cursor = conn.cursor()
query= "INSERT INTO users VALUES
('{user_name}',{password})"
cursor.execute(query.format(user_name=user-
name, password=password))
conn.commit()
是否可以检查html表单中的用户名和密码是否在数据库中匹配?
【问题讨论】:
-
正确使用cursor.execute 避免SQL 错误(也就是不要使用
query.format)。 -
并且不要以明文形式存储用户密码。使用salted hash!
标签: python mysql scripting pymysql