I'm struggling to append an id from mysql to the 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))
}
````
`Preformatted text`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

You can use:

http.Redirect(w, r, newUrl, http.StatusSeeOther)

https://golang.org/pkg/net/http/#Redirect

Maybe you can use this method for get last insert ID, and do not request another query

id, err := res.LastInsertId()
if err != nil {
     return err
}

Great! And what would be the proper way of appending this id in the URL?
Is there a function to do that?

put the id into the URL param, if the id is integer do you have converter them

http.Redirect(w, r, "/ployer/your-route/" + id, http.StatusSeeOther)

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.