提交哈希到 SQLite 数据库时出错,使用的是 Golang 语言。

huangapple go评论105阅读模式
英文:

Error submitting a hash to sqlite db golang

问题

尝试将表单中的哈希值发送到SQLite数据库

  1. func Signup(r http.ResponseWriter, w *http.Request) {
  2. w.ParseForm()
  3. email := w.Form.Get("Email")
  4. if !govalidator.IsEmail(email) {
  5. utils.ServerResponse("email is incorrect", "frontend/index.html", r)
  6. return
  7. }
  8. password := w.Form.Get("Password")
  9. hash, err := bcrypt.GenerateFromPassword([]byte("Password"), bcrypt.DefaultCost)
  10. if password == "" || len(password) < 6 {
  11. utils.ServerResponse("Password length must be longer than 6 letters and must contain numbers and uppercase letters", "frontend/index.html", r)
  12. return
  13. }
  14. query := fmt.Sprintf("INSERT INTO User (email, password) VALUES ('%s', '%s')", email, string(hash))
  15. fmt.Println(query, string(hash)) // 用于调试的打印语句
  16. statement , err := db.SqliteDatabase.Prepare(query)

终端输出并且没有提交到数据库:

  1. INSERT INTO User (email, password)
  2. VALUES ("email" , "hash")
  3. %!(EXTRA string=test@example.com, string=21818921) $2a$10$S5DObh/Kwbnl1JMvb.Xzf.Vo9SJsOMDaFugWstf/1xGCPTnZBxr3y
  4. near "%": 语法错误
英文:

Trying to send a hash from a form to an sqlite database

  1. func Signup(r http.ResponseWriter, w *http.Request) {
  2. w.ParseForm()
  3. email := w.Form.Get(&quot;Email&quot;)
  4. if !govalidator.IsEmail(email) {
  5. utils.ServerResponse(&quot;email is incorrect&quot;, &quot;frontend/index.html&quot;, r)
  6. return
  7. }
  8. password := w.Form.Get(&quot;Password&quot;)
  9. hash, err := bcrypt.GenerateFromPassword([]byte(&quot;Password&quot;), bcrypt.DefaultCost)
  10. if password == &quot;&quot; || len(password) &lt; 6 {
  11. utils.ServerResponse(&quot;Password length must be longer than 6 letters and must contain numbers and uppercase letters&quot;, &quot;frontend/index.html&quot;, r)
  12. return
  13. }
  14. query := fmt.Sprintf(&quot;INSERT INTO User (email, password)&quot;, VALUES &quot;email&quot;, &quot;hash&quot;)
  15. fmt.Println(query, string(hash)) // put this here for debugging
  16. statement , err := db.SqliteDatabase.Prepare(query)

Output in the terminal and nothing submits to the database:

  1. INSERT INTO User (email, password)
  2. VALUES (&quot;email&quot; , &quot;hash&quot;)
  3. %!(EXTRA string=test@example.com, string=21818921) $2a$10$S5DObh/Kwbnl1JMvb.Xzf.Vo9SJsOMDaFugWstf/1xGCPTnZBxr3y
  4. near &quot;%&quot;: syntax error

答案1

得分: 1

发现我在使用fmt.Sprintf函数时出现了错误,并进行了修复。

  1. query := fmt.Sprintf(`
  2. INSERT INTO User (email, password)
  3. VALUES ("%s" , "%s")
  4. `, email, hash)
英文:

Discovered i was using the fmt.Sprinf function incorrectly and fixed it.

  1. query := fmt.Sprintf(`
  2. INSERT INTO User (email, password)
  3. VALUES (&quot;%s&quot; , &quot;%s&quot;)
  4. `, email, hash)

huangapple
  • 本文由 发表于 2021年8月12日 07:50:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/68750107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定