预处理失败,请问是哪里出错?
package main

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

    "database/sql"
    "fmt"
)

var a string
var th string
var c string

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

func main() {
    server:=http.Server{Addr:":8090"}
    http.HandleFunc("/",welcome)
    http.HandleFunc("/upload",upload)
    db,err:=sql.Open("mysql","root:root@tcp(localhost:8090)/application")
    defer func() {
        db.Close()
    }()
    if err!=nil {
        fmt.Println("数据库连接失败")
        return
    }
    stmt,err:=db.Prepare("insert into app values (default,?,?,?)")
    defer func() {
        stmt.Close()
    }()
    if err!=nil {
        fmt.Println(err)
        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
    }
    if count>0 {
        fmt.Println("新增成功")
    }else {
        fmt.Println("新增失败")
    }
    server.ListenAndServe()
}

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问