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函数里面,能取到传过来的几个参数吗

    评论

报告相同问题?

悬赏问题

  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码