gpt4 book ai didi

html - Go r.FormValue 为空

转载 作者:IT王子 更新时间:2023-10-29 00:59:46 32 4
gpt4 key购买 nike

我得到了一个 html 表单,我可以在其中输入值并将它们插入到数据库中。但是当我插入它们时,值会像“”一样出现在数据库中。

这是我插入值的方式:

title, author, description := r.Form("title"), r.FormValue("author"), r.FormValue("description")

fmt.Println(title, author, description)

rows, err := db.Query("INSERT INTO apps (title, author, description) VALUES ($1, $2, $3)",
title, author, description)
PanicIf(err)

defer rows.Close()

http.Redirect(w, r, "/myapps", http.StatusFound)

db.Close()

HTML :

<form action="/apps" method="POST">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="title" />
</div>
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control" name="author" />
</div>
<div class="form-group">
<label>Description</label>
<input type="text" class="form-control" name="description" />
</div>

<input type="submit" value="Create" class="btn btn-success" />
<a href="/myapps">
<input type="button" value="Return" class="btn btn-primary" />
</a>
</form>

可能是表单值有问题?

最佳答案

你必须调用r.ParseForm()首先在你可以使用 r.Form/r.PostForm 之前。

发件人:http://golang.org/src/pkg/net/http/request.go#L168

// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values

// PostForm contains the parsed form data from POST or PUT
// body parameters.
// This field is only available after ParseForm is called.
// The HTTP client ignores PostForm and uses Body instead.
PostForm url.Values

关于html - Go r.FormValue 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24723038/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com