英文:
Error submitting a hash to sqlite db golang
问题
尝试将表单中的哈希值发送到SQLite数据库
func Signup(r http.ResponseWriter, w *http.Request) {
w.ParseForm()
email := w.Form.Get("Email")
if !govalidator.IsEmail(email) {
utils.ServerResponse("email is incorrect", "frontend/index.html", r)
return
}
password := w.Form.Get("Password")
hash, err := bcrypt.GenerateFromPassword([]byte("Password"), bcrypt.DefaultCost)
if password == "" || len(password) < 6 {
utils.ServerResponse("Password length must be longer than 6 letters and must contain numbers and uppercase letters", "frontend/index.html", r)
return
}
query := fmt.Sprintf("INSERT INTO User (email, password) VALUES ('%s', '%s')", email, string(hash))
fmt.Println(query, string(hash)) // 用于调试的打印语句
statement , err := db.SqliteDatabase.Prepare(query)
终端输出并且没有提交到数据库:
INSERT INTO User (email, password)
VALUES ("email" , "hash")
%!(EXTRA string=test@example.com, string=21818921) $2a$10$S5DObh/Kwbnl1JMvb.Xzf.Vo9SJsOMDaFugWstf/1xGCPTnZBxr3y
near "%": 语法错误
英文:
Trying to send a hash from a form to an sqlite database
func Signup(r http.ResponseWriter, w *http.Request) {
w.ParseForm()
email := w.Form.Get("Email")
if !govalidator.IsEmail(email) {
utils.ServerResponse("email is incorrect", "frontend/index.html", r)
return
}
password := w.Form.Get("Password")
hash, err := bcrypt.GenerateFromPassword([]byte("Password"), bcrypt.DefaultCost)
if password == "" || len(password) < 6 {
utils.ServerResponse("Password length must be longer than 6 letters and must contain numbers and uppercase letters", "frontend/index.html", r)
return
}
query := fmt.Sprintf("INSERT INTO User (email, password)", VALUES "email", "hash")
fmt.Println(query, string(hash)) // put this here for debugging
statement , err := db.SqliteDatabase.Prepare(query)
Output in the terminal and nothing submits to the database:
INSERT INTO User (email, password)
VALUES ("email" , "hash")
%!(EXTRA string=test@example.com, string=21818921) $2a$10$S5DObh/Kwbnl1JMvb.Xzf.Vo9SJsOMDaFugWstf/1xGCPTnZBxr3y
near "%": syntax error
答案1
得分: 1
发现我在使用fmt.Sprintf
函数时出现了错误,并进行了修复。
query := fmt.Sprintf(`
INSERT INTO User (email, password)
VALUES ("%s" , "%s")
`, email, hash)
英文:
Discovered i was using the fmt.Sprinf function incorrectly and fixed it.
query := fmt.Sprintf(`
INSERT INTO User (email, password)
VALUES ("%s" , "%s")
`, email, hash)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论