<html>
<body>
<input type="checkbox" id="c1" name="custopt[1]" value="Option1"/>
<input type="checkbox" id="c2" name="custopt[2]" value="Option2"/>
</body>
</html>
<?php
$custopt = (\filter_input(\INPUT_POST,'custopt'));
for($x=1;$i<=2;$x++)
{
if(empty($custopt[$x])
{
/* DO NOT INSERT */
}
else
{ /* @var $sql type */
$sql="insert into starterveg(itemId,title,subtitle,descript,availability,price,customoption,r1,r2)values
('$itemId','$title','$subtitle','$descript','$availability','$price','$custopt[$x]','$r1','$r2')";
/* @var $result type */
$result= \mysqli_query($dbhandle,$sql) or die(\mysqli_error($dbhandle));
/* @var $custop type */
}}
?>
我有一个问题,数据库中的复选框字段为空,我需要复选框的相应值才能存储。现在我已经更新了代码。现在我将O / P作为成功连接样品成功
答案 0 :(得分:0)
我假设您在循环中动态列出了您的复选框。如果是这样,试试这个:
$counter=1;
while( /* your condition */){
echo "<input type='checkbox' id='c1' name='custopt[]' value='Option1'/>
echo "<input type='hidden' value='$counter' name='hiddencounter'>";
$counter=$counter+1;
}
提交时:
$getcount=$_POST['hiddencounter'];
$custopt=$_POST['custopt'];
for($x=1;$x<=$getcount;$x++){
if(empty($custopt[$x])){
/* DO NOT INSERT */
}
else {
mysqli_query($dbhandle,"INSERT INTO starterveg (itemId, title, subtitle, descript, availability, price, customoption, r1, r2)
VALUES
('$itemId','$title','$subtitle','$descript','$availability','$price','$custopt[$x]','$r1','$r2')";
}
} /* END OF FOR LOOP */
<强>更新强>
HTML文件:
<form action='your.php' method='POST'>
<input type="checkbox" id="c1" name="custopt[0]" value="Option1"/>
<input type="checkbox" id="c2" name="custopt[1]" value="Option2"/>
<input type="submit">
</form>
your.PHP文件:
$custopt=$_POST['custopt'];
for($x=0;$x<=1;$x++){
if(empty($custopt[$x])){
/* DO NOT INSERT */
}
else {
mysqli_query($dbhandle,"INSERT INTO starterveg (itemId, title, subtitle, descript, availability, price, customoption, r1, r2)
VALUES
('$itemId','$title','$subtitle','$descript','$availability','$price','$custopt[$x]','$r1','$r2')";
}
} /* END OF FOR LOOP */