php 使用 HTML 复选框将 1 或 0 放入 MySQL 表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3675900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using an HTML checkbox to put 1 or 0 into a MySQL table
提问by John
I am using a simple HTML checkbox in a form to put a 1 for checked and a 0 for unchecked in a field called "subcheck" in a MySQL table.
我在表单中使用一个简单的 HTML 复选框,在 MySQL 表中名为“subcheck”的字段中放置 1 表示已选中,0 表示未选中。
Does the checkbox default to 1 for "checked" and 0 for unchecked? If not, how can I give it those values?
复选框是否默认为“已选中”为 1,未选中为 0?如果没有,我怎么能给它这些值?
Form:
形式:
<div class="subcheck"><INPUT TYPE=CHECKBOX NAME="subcheck">Click here to receive free money in the mail.<P></div>
In the file the form goes to:
在文件中,表格转到:
$subcheck = $_POST['subcheck'];
mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$slug', '$cleanurl', '$displayurl', NULL, '$subcheck')");
The MySQL table:
MySQL表:
`submission` (
`submissionid` int(11) unsigned NOT NULL auto_increment,
`loginid` int(11) NOT NULL,
`title` varchar(1000) NOT NULL,
`slug` varchar(1000) NOT NULL,
`url` varchar(1000) NOT NULL,
`displayurl` varchar(1000) NOT NULL,
`datesubmitted` timestamp NOT NULL default CURRENT_TIMESTAMP,
`subcheck` tinyint(1) NOT NULL,
PRIMARY KEY (`submissionid`)
)
回答by Tom Sarduy
To be sure that always 1 or 0 is sent, you can insert an input hidden with the same name
of the checkbox in the html:
为了确保始终发送 1 或 0,您可以插入一个隐藏的输入,该输入与name
html 中的复选框相同:
//The input hidden
<input type="hidden" name="subcheck" value="0" />
//The checkbox
<input type="checkbox" name="subcheck" value="1" />
This way, you don't need to check in server side if the textbox is set or not ;)
这样,无论是否设置了文本框,您都不需要检查服务器端;)
回答by Nev Stokes
If the checkbox isn't ticked then nothing is sent to the server. Instead, you can provide a default value with:
如果未勾选复选框,则不会向服务器发送任何内容。相反,您可以提供一个默认值:
$subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
回答by Ljubomir ?oki?
If Checkbox is unchecked then $subcheck is not submited at all.
如果未选中复选框,则根本不会提交 $subcheck。
In PHP you should write:
在 PHP 中你应该写:
if !(isset($_POST['subcheck']))
$subcheck = 0;
mysql_query("INSERT INTO submission VALUES (NULL, '$uid', '$title', '$slug', '$cleanurl', '$displayurl', NULL, '$subcheck')");
回答by Tejas Patel
u should try if statement or ternarycondition statement.. if(isset($_POST['subcheck'])) $subcheck = 1; else $subcheck = 0;
你应该尝试 if 语句或三元条件语句.. if(isset($_POST['subcheck'])) $subcheck = 1; 否则 $subcheck = 0;
or u can try $subcheck = (isset($_POST['subcheck'])) ? 1 : 0;
或者你可以试试 $subcheck = (isset($_POST['subcheck'])) ?1:0;
and in database u should use enum type for subcheck
并且在数据库中你应该使用枚举类型进行子检查
回答by i.jolly
This can be used as follows
这可以按如下方式使用
on form page
在表单页面
<input type="checkbox" name="status" value="1" >
on processing page
在处理页面
$status = (isset($_REQUEST['status']));
if ($status == 1 )
{
$status = 1;
}
else
{
$status = 0;
}
echo $status;