根据复选框值将“是”或“否”插入 MySQL
我有一个如下所示的复选框列表,数字 416、419 等代表页码。它们用于 2 组字段,一组是“likebutton”,另一组是“sendbutton”。
<input type="checkbox" name="likebutton[]" value="416" />
<input type="checkbox" name="likebutton[]" value="417" />
<input type="checkbox" name="likebutton[]" value="418" />
<input type="checkbox" name="likebutton[]" value="419" />
...more checkboxes
<input type="checkbox" name="sendbutton[]" value="416" />
<input type="checkbox" name="sendbutton[]" value="417" />
<input type="checkbox" name="sendbutton[]" value="418" />
<input type="checkbox" name="sendbutton[]" value="419" />
...more checkboxes
我想要实现的是,当这些复选框被选中时,我能够使用 MySQL 中的 likebutton 和 sendbutton 字段的“是”(选中)或“否”(未选中)数据更新每一行(页面)。目前我的 mysql 查询如下所示。这似乎仅在我有一个 foreach 函数并且勾选一个或多个复选框时才起作用,但如果我保留下面所述的代码或如果我未选中所有内容,则会收到 foreach 函数的错误。
foreach($_POST["likebutton"] as $pagenumberL)
$YesNoL = isset($_POST["likebutton"]) ? 'Yes' : 'No';
mysql_query("UPDATE pages SET likebutton='".$YesNoL."' WHERE page_id='".$pagenumberL."'");
foreach($_POST["sendbutton"] as $pagenumberS)
$YesNoS = isset($_POST["sendbutton"]) ? 'Yes' : 'No';
mysql_query("UPDATE pages SET sendbutton='".$YesNoS."' WHERE page_id='".$pagenumberS."'");
关于我的问题的任何指示或关于我应该如何进行的更好建议?
I have a list of checkboxes that look like below, the numbers, 416, 419, etc represent a page number. They are for 2 sets of fields, one is "likebutton", the other is "sendbutton".
<input type="checkbox" name="likebutton[]" value="416" />
<input type="checkbox" name="likebutton[]" value="417" />
<input type="checkbox" name="likebutton[]" value="418" />
<input type="checkbox" name="likebutton[]" value="419" />
...more checkboxes
<input type="checkbox" name="sendbutton[]" value="416" />
<input type="checkbox" name="sendbutton[]" value="417" />
<input type="checkbox" name="sendbutton[]" value="418" />
<input type="checkbox" name="sendbutton[]" value="419" />
...more checkboxes
What I am trying to achieve is when these checkboxes get checked, I am able to update each row (page) with the data of either Yes (Checked) or No (Unchecked) for both likebutton and sendbutton fields in MySQL. Currently my mysql query look like something below. This seems to work only when I have one foreach function and if I tick off one or more checkboxes, but if i leave the codes as stated below or If I have everything unchecked, I get an error for the foreach function.
foreach($_POST["likebutton"] as $pagenumberL)
$YesNoL = isset($_POST["likebutton"]) ? 'Yes' : 'No';
mysql_query("UPDATE pages SET likebutton='".$YesNoL."' WHERE page_id='".$pagenumberL."'");
foreach($_POST["sendbutton"] as $pagenumberS)
$YesNoS = isset($_POST["sendbutton"]) ? 'Yes' : 'No';
mysql_query("UPDATE pages SET sendbutton='".$YesNoS."' WHERE page_id='".$pagenumberS."'");
Any pointer on my problem or better suggestion as to how I should proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
复选框的问题(正如您所指出的)是未选中的复选框不会提交任何数据。
解决这个问题的标准技巧是......
当选中该复选框时,它的值将按照文档稍后显示的方式提交。如果未选中,则提交隐藏的输入值。
然后,您可以简单地按名称检查值
在您的特定情况下,在输入名称中包含
page_id
,例如在 PHP 中
The problem with checkboxes (as you have noted) is that unchecked ones do not submit any data.
The standard trick to get around this is...
When the checkbox is checked, its value is submitted as it appears later in the document. If unchecked, the hidden input value is submitted.
You can then simply check the value by name
In your particular case, include the
page_id
in the input name, egAnd in PHP