weixin_45134380 2019-07-22 22:19 采纳率: 0%
浏览 229

未报错,但无法把客户端数据写入数据库中

请大神解疑:为什么无法进入到localhost:8090/upload界面 且 为什么无法把客户端输入数据写入到数据库中?

以下是后端代码:

package main

import (
    "net/http"
    "html/template"
    _ "github.com/go-sql-driver/mysql"
    "database/sql"
    "fmt"
)

func welcome(w http.ResponseWriter,r *http.Request)  {
    t,_:=template.ParseFiles("static/application.html")
    t.Execute(w,nil)
}

func upload(w http.ResponseWriter,r *http.Request)  {
    t,_:=template.ParseFiles("static/success.html")
    t.Execute(w,nil)
    a:=r.FormValue("applicant")
    th:=r.FormValue("theme")
    c:=r.FormValue("content")
    //连接mysql实现数据新增
    //1.打开连接
    db,err:=sql.Open("mysql","root:root@tcp(localhost:8090)/application")
    db.Ping()
    defer func() {
        db.Close()
    }()
    if err!=nil {
        fmt.Println("数据连接失败")
        return
    }
    //2.预处理SQL
    stmt,err:=db.Prepare("insert into application values (default ,?,?,?)")
    defer func() {
        stmt.Close()
    }()
    if err!=nil {
        fmt.Println("预处理失败")
        return
    }
    result,err:=stmt.Exec(a,th,c)
    if err!=nil {
        fmt.Println("sql执行失败")
        return
    }
    count,err:=result.RowsAffected()
    if err!=nil {
        fmt.Println("结果获取失败")
        return
    }
    //3.获取结果
    if count>0 {
        fmt.Println("新增成功")
    }else {
        fmt.Println("新增失败")
    }
    //4.关闭连接(以上defer函数实现关闭连接)
}

func main() {
    server:=http.Server{Addr:":8090"}
    http.HandleFunc("/",welcome)
    http.HandleFunc("/upload",upload)
    server.ListenAndServe()
}

以下是前端代码:
图片说明图片说明
图片说明

  • 写回答

1条回答 默认 最新

  • 关注

    upload函数里面,能取到传过来的几个参数吗

    评论

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的