[Golang]一些书城项目中出现错误的原因和解决办法(二)


跟着B站尚硅谷的GoWeb教程写书城项目,整理一下自己写的时候出现的错误和解决办法。


  • 错误三:数据库的 cart_items 表中 total_count 始终为 0。

    • 原因:更新购物车信息的 UpdateCart 函数中的 sql 语句写错,如下:

      update carts set total_count = ? and total_amount = ? where id = ?
      
    • 解决办法:根据 MySQL 语法,应把 total_count = ? and total_amount = ? 中的 and 改为 ,

      如下:

      update carts set total_count = ? and total_amount = ? where id = ?
      
    • 附 MySQL 的基础增删改查语法:

      insert into 表名(字段a, 字段b) values(值a, 值b) where 正则表达式
      delete from 表名  where 正则表达式
      update 表名 set 字段a=值a, 字段b=值b where 正则表达式
      select 字段a,字段b from 表名 where 正则表达式
      -- select * from 表名 where 正则表达式
      -- 返回所有的字段值
      
  • 错误四:cart.html 无法正常加载,

    • 原因:有可能是传入参数改变(以我的情况为例,传入参数由 cart 变成 session)
    • 解决办法:相应做出改变即可,(在原来的 .CartItems 前加上 .Cart