I wanted to write data from the form to the database(different tables), but for some reason the images are not recorded.
func save_obj(w http.ResponseWriter, r *http.Request) {
title := r.FormValue("title")
type_obj := r.FormValue("type_obj")
location := r.FormValue("location")
long := r.FormValue("long")
fond := r.FormValue("fond")
//video := r.FormValue("video")
inf := r.FormValue("inf")
pros := r.FormValue("pros")
about := r.FormValue("about")
//docs := r.FormValue("docs")
//подключение
db, err := sql.Open("mysql", "root:root@tcp(127.0.0.1:8889)/service")
if err != nil {
panic(err)
}
defer db.Close()
//установка
insert, err := db.Query(fmt.Sprintf("INSERT INTO `objects` (`title`, `type_obj`, `location`, `long`, `fond`, `inf`, `pros`, `about`)"+
" VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", title, type_obj, location, long, fond, inf, pros, about))
if err != nil {
panic(err)
}
defer insert.Close()
imgOne := b64.URLEncoding.DecodeString(r.FormValue("img-1"))
imgTwo := b64.URLEncoding.DecodeString(r.FormValue("img-2"))
imgThree := b64.URLEncoding.DecodeString(r.FormValue("img-3"))
imgFour := b64.URLEncoding.DecodeString(r.FormValue("img-4"))
ins, er := db.Query(fmt.Sprintf("INSERT INTO `img-1` (`title`, `img`) VALUES('%s', '%s')", title, imgOne))
if er != nil {
panic(er)
}
defer ins.Close()
insr, error := db.Query(fmt.Sprintf("INSERT INTO `img-2` (`title`, `img`) VALUES('%s', '%s')", title, imgTwo))
if error != nil {
panic(error)
}
defer insr.Close()
in, Error := db.Query(fmt.Sprintf("INSERT INTO `img-3` (`title`, `img`) VALUES('%s', '%s')", title, imgThree))
if Error != nil {
panic(Error)
}
defer in.Close()
//последнее изображение
in, Error := db.Query(fmt.Sprintf("INSERT INTO `img-4` (`title`, `img`) VALUES('%s', '%s')", title, imgFour))
if Error != nil {
panic(Error)
}
defer in.Close()
http.Redirect(w, r, "/create_obj", http.StatusSeeOther)
}
Everything except the image recording works fine, but when I try to record, an error is displayed:
cannot use r.FormValue("img-1") (type string) as type []byte in argument to base64.URLEncoding.EncodeToString html:
<input type="file" class="img" name="img-1"><br>
<input type="file" class="img" name="img-1"><br>
<input type="file" class="img" name="img-3"><br>
<input type="file" class="img" name="img-4"><br>
Why don't the types match and how can I encrypt an image in a blob?