mardi 8 janvier 2019

Golang - how to insert data from checbox into database

please help me, how can i get data from checbox (array), and put into database, i only get this, but not post into database :

enter image description here

// This is my main.go

func Insert(w http.ResponseWriter, r *http.Request) {
db := dbConn()
if r.Method == "POST" {
    r.ParseForm()
    item_code := r.FormValue("item_code")
    item_name := r.FormValue("item_name")

    location := r.Form["location"]

    // // Form submitted
    // r.ParseForm() // Required if you don't call r.FormValue()
    // fmt.Println(r.Form["location"])

    insForm, err := db.Prepare("INSERT INTO master_code(item_code, item_name, location) VALUES(?,?,?)")

    fmt.Println(location)

    // names := []string{location}
    // fmt.Println(names)
    if err != nil {
        panic(err.Error())
    }
    insForm.Exec(item_code, item_name)
    insForm.Exec(location)
    log.Println("INSERT: Item Code: " + item_code + " | Item Name: " + item_name + "")
}
defer db.Close()
http.Redirect(w, r, "/", 301)

}

//this is my html

<div class="form-group col-md-6">
                                    <label for="inputPassword4">Location</label><br>
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="checkbox" id="inlineCheckbox1" name="location" value="Jakarta">
                                        <label class="form-check-label" for="inlineCheckbox1">Jakarta</label>
                                    </div>
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="checkbox" id="inlineCheckbox2" name="location" value="Surabaya">
                                        <label class="form-check-label" for="inlineCheckbox2">Surabaya</label>
                                    </div>
                                    <div class="form-check form-check-inline">
                                        <input class="form-check-input" type="checkbox" id="inlineCheckbox3" name="location" value="Tangerang">
                                        <label class="form-check-label" for="inlineCheckbox2">Tangerang</label>
                                    </div>




Aucun commentaire:

Enregistrer un commentaire