Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

php - Using an HTML checkbox to put 1 or 0 into a MySQL table

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.

Does the checkbox default to 1 for "checked" and 0 for unchecked? If not, how can I give it those values?

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:

`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`)
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

//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 ;)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...