通常不知道如何"插入"复选框进入数据库,我有一个带有几个复选框的表单...大约25个复选框,每个复选框具有相同的名称和1-25的值,现在我需要它们具有相同的名称,以便运行一个脚本,防止选择不超过4个复选框...
HTML代码
<input type="checkbox" name="ckb" id="EngineeringWorkshops" value=1 onclick='chkcontrol(0)';>
<label for="EngineeringWorkshop"> Engineering Workshops </label>
PHP代码
$engineeringworkshops = mysqli_real_escape_string($dbcon, $_POST['cob']);
$sql="INSERT INTO courses (studentid, engworkshops)
VALUES ('$studentid', '$engineeringwokshops')";
engworkshops列类型是tinyint(4)默认值设置为0
答案 0 :(得分:1)
使用这样的html:
<form>
<input type='checkbox' name='data[]' value='YourId' /> Data
<input type='checkbox' name='data[]' value='AnotherId' /> AnotherData
<input type='checkbox' name='data[]' value='OneMoreId' /> OneMoreData
// others
</form>
如何在php中处理这些数据?
使用此:
$engineeringworkshops = mysqli_real_escape_string($dbcon, $_POST['cob']);
// handle data from checkboxes
foreach($_POST['data') as $item) {
// do something in item
$sql="INSERT INTO courses (studentid, engworkshops) VALUES ('$item', '$engineeringwokshops')";
}