我正在为装有python和flask的购物车工作。我希望当用户单击某项的订购按钮时,将他们重定向到/ cart / add /路由,在该路由中运行SQL语句以基于用户名从用户表中检索用户名,从所有食品表中检索项目名称和价格等数据。在Delivery_address表的URL和地址中传递的项目ID。一旦收集到此信息,它还将被添加到shopping_cart表中,在该表中将被临时保存,直到我确定下一步为止。我在使用SQL时遇到问题,希望有人可以纠正它,因为它给了我一个错误。
"INSERT INTO shopping_cart_tbl VALUES (username, address, item, cost) \
SELECT user.username, addr.address, food.item_, food.cost \
FROM register_tbl AS user FULL JOIN delivery_addresses_tbl AS addr FULL JOIN allfood_tbl AS food \
ON user.username = addr.username AND food.id=%s"
为了知道我是否可以通过任何建议改进代码,我将发布该路线的代码
@app.route('/cart/add/<item_id>', methods=['POST', 'GET'])
def sadd_to_cart(item_id):
# do a cross join of the three tables maybe to retrieve the necessarinformation
if 'userkey' in session:
con = pymysql.connect("localhost", "root", "", "sampledb")
cursor_full_join = con.cursor()
sql="INSERT INTO shopping_cart_tbl VALUES (username, address, item, cost) \
SELECT user.username, addr.address, food.item_, food.cost \
FROM register_tbl AS user FULL JOIN delivery_addresses_tbl AS addr FULL JOIN allfood_tbl AS food \
ON user.username = addr.username AND food.id=%s"
cursor_full_join.execute(sql, item_id)
return render_template('homepage.html')
elif 'userkey' not in session:
return redirect('/login')
else:
return redirect('/login')
感谢您的帮助。 编辑:我删除了不必要的引号。抱歉。我出于某种原因没有注意到他们