Html 如何在带有把手的表单中向SQL中插入复选框?
我有一个编辑产品的表单: 当它被编辑时,它会通过更新这个SQL表(称为product)来上传它 当我想发布复选框时,问题就出现了。在表单中,除了始终获取值0的复选框外,所有内容都已正确提交 这是console.log的屏幕截图,复选框是可用值,它是一个布尔值 在表格中,复选框为:Html 如何在带有把手的表单中向SQL中插入复选框?,html,mysql,node.js,express,handlebars.js,Html,Mysql,Node.js,Express,Handlebars.js,我有一个编辑产品的表单: 当它被编辑时,它会通过更新这个SQL表(称为product)来上传它 当我想发布复选框时,问题就出现了。在表单中,除了始终获取值0的复选框外,所有内容都已正确提交 这是console.log的屏幕截图,复选框是可用值,它是一个布尔值 在表格中,复选框为: <div class="form-group"> <input type="checkbox" name="is_available" {{#if product.is_available}}c
<div class="form-group">
<input type="checkbox" name="is_available" {{#if product.is_available}}checked{{/if}}>
</div>
我想知道,当复选框处于打开/关闭状态时,为什么数据库中的复选框值没有更新。。复选框没有值属性,如果选中该属性,该属性将发送值“打开”。 如果未选中该复选框,它将不会在post请求中发送该值 您可以做的是更改接收请求的函数,并根据POST请求中发送的值将is_available更改为0或1。if(newProduct.is_available==“on”){newProduct.is_available=1;}else{newProduct.is_available=0;}
router.post('/edit/:id', isLoggedIn, async(req,res) => {
const { id } = req.params;
const { title, url, image_path, description, price, coupon, discount, is_available } = req.body;
const newProduct = {
title,
url,
image_path,
description,
price,
coupon,
discount,
is_available
};
await pool.query('UPDATE product SET ? WHERE id = ?', [newProduct, id]);
req.flash('success', 'Product modified successfully');
res.redirect('/products/');
});