普通网友 2019-04-22 22:26
浏览 45
已采纳

我正在努力将mysql的ID附加到URL

I'm trying to append an id (and other info) to the url, so I can access it later, but I can't find the right method after some research.

I've tried to use Get() method, query(), Add(), but I couldn't redirect the URL.

var email_ployer string

func RegisterNewPloyer(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path != "/ployer/register" {
        http.Error(w, "404 not found.", http.StatusNotFound)
        return
    }
    db := connect.ConnectDB()
    defer db.Close()
    switch r.Method {
    case "POST":
        email_ployer = r.FormValue("email")
        senha := r.FormValue("senha")
        senha, _ = HashPassword(senha)
        tx, _ := db.Begin()
        stmt, _ := tx.Prepare("INSERT INTO ployers(email_ployer, senha_ployer) VALUES(?,?)")
        _, erro := stmt.Exec(email_ployer, senha)
        if erro != nil {
            tx.Rollback()
            log.Fatal(erro)
        }
        tx.Commit()
    }
    Redirect(w, r)
}

func Redirect(w http.ResponseWriter, r *http.Request) {
    db2 := connect.ConnectDB()
    defer db2.Close()
    var id string
    tx, _ := db2.Begin()
    rows, _ := tx.Query("SELECT id FROM ployers WHERE email_ployer = '?'", email_ployer)
    for rows.Next() {
        if err := rows.Scan(&id); err != nil {
            log.Fatal(err)
        }
        if err := rows.Err(); err != nil {
            log.Fatal(err)
        }
    }
    http.Redirect(w, r, x, http.StatusSeeOther)
}




func main() {
    http.HandleFunc("/ployer/seja-um-de-nos", LoadPloyerContent)
    http.HandleFunc("/ployer/register", register.RegisterNewPloyer)
    http.HandleFunc("/ployer/complete/", LoadPloyerContent)
    http.HandleFunc("/ployer/register-received", LoadPloyerContent)
    log.Fatal(http.ListenAndServe(":3306", nil))
}

In my system, I want the user to register his E-mail and password, create an new user in the DB and redirect the URL to something like localhost:3306/ployer/complete/id

  • 写回答

2条回答 默认 最新

  • duangong1979 2019-04-22 23:23
    关注

    Just use res.LastInsertId() to get the id and pass it to your redirect function, and build the url:

    func RegisterNewPloyer(w http.ResponseWriter, r *http.Request) {
      //...
      //...
      res, erro := stmt.Exec(email_ployer, senha)
      if erro != nil {
        tx.Rollback()
        log.Fatal(erro)
      }
      tx.Commit()
      id, erro := res.LastInsertId()
      if erro != nil {
        log.Fatal(erro)
      }
      Redirect(w, r, id)
    }
    
    func Redirect(w http.ResponseWriter, r *http.Request, id int64) {
        uri := fmt.Sprintf("http://localhost:3306/ployer/complete/%d", id)
        http.Redirect(w, r, uri, http.StatusSeeOther)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题