gpt4 book ai didi

html - 我无法在 HTML 上查看我的所有 MySQL 表数据

转载 作者:行者123 更新时间:2023-12-04 08:05:38 25 4
gpt4 key购买 nike

我从 MySQL 表中获取了所有数据,并使用“日志”功能将其打印出来,但我需要将其打印在 HTML 页面上。我只能打印一列。我想打印所有产品数据。我怎样才能做到这一点?
简而言之:我想列出商店页面的所有产品,但我只能列出一种产品。 (我将添加数百种产品。目前,我的表中只有 3 种产品。)
我已经 2 天没有解决它了,如果您能提供帮助,我将非常高兴。非常感谢...
转到文件

package main

import ( "database/sql" "fmt" "log" "net/http" "text/template"

_ "github.com/go-sql-driver/mysql"
)

type product struct { ID int Name string Stock int Price float32 }

var products product

func handler(w http.ResponseWriter, r *http.Request) {

products = product{
ID: products.ID,
Name: products.Name,
Stock: products.Stock,
Price: products.Price,
}

appointment, _ := template.ParseFiles("ot.html")
appointment.Execute(w, products)
}

func main() { fmt.Println("Go MySQL Tutorial")

sqlType := "mysql"
sqlUsn := "root"
sqlPass := "2582064"
sqlDatabase := "gowdb"

db, err := sql.Open(sqlType, sqlUsn+":"+sqlPass+"@/"+sqlDatabase)
if err != nil {
panic(err.Error())
}

defer db.Close()

results, err := db.Query("SELECT * FROM products")
if err != nil {
panic(err.Error())
}

for results.Next() {

err = results.Scan(&products.ID, &products.Stock, &products.Price, &products.Name)
if err != nil {
panic(err.Error())
}

log.Println(products.Name)
}

if err != nil {
panic(err.Error())
}

http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
HTML 页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>PRODUCT 1</h1>
<div>NAME: {{.Name}}</div>
<div>PRICE: {{.Price}}</div>
<div>STOCK: {{.Stock}}</div>
<div>ID: {{.ID}}</div>
<div>BUY FROM: xxx.com</div>

<h1>PRODUCT 2</h1>
<div>NAME: {{.Name}}</div>
<div>PRICE: {{.Price}}</div>
<div>STOCK: {{.Stock}}</div>
<div>ID: {{.ID}}</div>
<div>BUY FROM: xxx.com</div>

<h1>PRODUCT 3</h1>
<div>NAME: {{.Name}}</div>
<div>PRICE: {{.Price}}</div>
<div>STOCK: {{.Stock}}</div>
<div>ID: {{.ID}}</div>
<div>BUY FROM: xxx.com</div>
</body>
</html>
日志输出(没问题)
电子掩膜
过滤面膜
飞机面具
HTML 页面上的输出
产品 1 名称:Aircrop mask 价格:19 股票:2 ID:2 购买自:xxx.com
产品 2 名称:Aircrop mask 价格:19 股票:2 ID:2 购买自:xxx.com
产品 3 名称:Aircrop mask 价格:19 股票:2 ID:2 购买自:xxx.com

最佳答案

所以我已经修复了您的代码,但由于我没有对其进行测试,因此对此有所保留。还请记住,原始代码中的一些内容显然是错误的。仅举一例,处理程序正在从全局变量中获取响应。这是一个灾难的秘诀,你需要“不使用”来做到这一点,否则它会给你带来更多的麻烦。
我能说的最好的就是让你的老师审查这段代码。
main.go

package main

import (
"database/sql"
"net/http"
"text/template"

_ "github.com/go-sql-driver/mysql"
)

type product struct {
ID int
Name string
Stock int
Price float32
}

func handler(w http.ResponseWriter, r *http.Request) {
appointment, _ := template.ParseFiles("ot.html")
appointment.Execute(w, GetProducts())
}

func main() {

http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}

func GetProducts() []product {
var out = make([]product, 0, 10)
sqlType := "mysql"
sqlUsn := "root"
sqlPass := "2582064"
sqlDatabase := "gowdb"

db, err := sql.Open(sqlType, sqlUsn+":"+sqlPass+"@/"+sqlDatabase)
if err != nil {
panic(err.Error())
}

defer db.Close()

results, err := db.Query("SELECT * FROM products")
if err != nil {
panic(err.Error())
}

for results.Next() {
var p product
err = results.Scan(&p.ID, &p.Stock, &p.Price, &p.Name)
if err != nil {
panic(err.Error())
}
out = append(out, p)
}

if err != nil {
panic(err.Error())
}
return out
}
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
{{ range $i, $e := . }}
<h1>PRODUCT $i</h1>
<div>NAME: {{.Name}}</div>
<div>PRICE: {{.Price}}</div>
<div>STOCK: {{.Stock}}</div>
<div>ID: {{.ID}}</div>
<div>BUY FROM: xxx.com</div>
{{end}}
</body>
</html>

关于html - 我无法在 HTML 上查看我的所有 MySQL 表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66233653/

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