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