【问题标题】:images are not recorded in the mysql database图像未记录在 mysql 数据库中
【发布时间】:2021-04-21 02:50:09
【问题描述】:

我想将表单中的数据写入数据库(不同的表),但由于某种原因没有记录图像。

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)
}

除图像记录外,一切正常,但是当我尝试记录时,显示错误:

不能在 base64.URLEncoding.EncodeToString 的参数中使用 r.FormValue("img-1") (type string) 作为 type []byte 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>

为什么类型不匹配?如何加密 blob 中的图像?

【问题讨论】:

  • FWIW,包括表/列标识符中的数学运算符是一个灾难性的坏主意。
  • 为什么不使用更简单的方法呢?将图片作为文件存储在任何文件夹中。并将图像路径存储在数据库中。好处包括:易于开发、消除图像转换处理以及每张图像的存储空间减少 30%。

标签: mysql go


【解决方案1】:

根据错误,r.FormValue("img-1")(也可能还有img-2img-3)的类型已经是string,但base64.URLEncoding.EncodeToString() 采用[]byte 参数。

由于您的表单使用表单类型file,您需要将其处理为multipart,并使用r.FormFile("img-1") 获取图像以获取字节:

file, header, err := r.FormFile("img-1")    
defer file.Close()
if err != nil {
    // handle error
}
b := bytes.NewBuffer(nil)
if _, err := io.Copy(b, file); err != nil {
    // handle error
}
// convert bytes to base64
img := base64.URLEncoding.EncodeToString(b.Bytes())

您可以使用fileheader 制作一个方便的函数。在 header 中,您可以找到文件名、大小和 mime/类型等元数据,这些元数据可以与图像一起存储到数据库以供进一步使用。

【讨论】:

  • 相应地更新了我的答案
  • "不能在 base64.URLEncoding.EncodeToString 的参数中使用 b (type *bytes.Buffer) 作为 type []byte"
  • 对不起,我是b.Bytes(),我正在头顶上用手机打字。试试b.Bytes(),它应该返回[]byte
  • 我写的是错误panic(err),现在错误是运行时错误
  • 错误“无效的内存地址或零指针取消引用”
【解决方案2】:

您的代码存在很多问题,但集中在将图像插入表格中。

  1. 你应该使用Exec(),而不是Query()
  2. 永远不要使用 fmt.Printf 和 %s 格式:使用 SQL 占位符 ?(尤其是当您的数据来自 HTML 表单时)(在 cmets 中已提及); MySQL 驱动程序知道如何处理字节
  3. 在 RDBMS 中存储图像是可行的,但通常是错误的方法

您应该如何保存已解码的 base64:

    ins, er := db.Exec("INSERT INTO `img-1` (`title`, `img`) VALUES(?, ?)",
        title, imgOne))
    if er != nil {
        panic(er)
    }

另外,考虑返回错误,将其命名为err(而不是er)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多