请大神解疑:为什么无法进入到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()
}
以下是前端代码: